bashrc if: 表达式语法错误

5
我已经编写了以下的 .bashrc 文件:
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions


function up( )
{
LIMIT=$1
P=$PWD
for ((i=1; i <= LIMIT; i++))
do
    P=$P/..
done
cd $P
export MPWD=$P
}

function back( )
{
LIMIT=$1
P=$MPWD
for ((i=1; i <= LIMIT; i++))
do
    P=${P%/..}
done
cd $P
export MPWD=$P
}

然而,在保存之后,当我执行source .bashrc时,我遇到了以下错误:if: Expression Syntax.

我做错了什么?我谷歌搜索了一段时间,但没有结果。


2
使用echo $SHELL检查您的shell是否为bash。 - JohnTortugo
@JohnTortugo 谢谢,我一直在使用c shell并在bashrc中进行更改。 - Chani
1个回答

11
if: Expression Syntax 

这不是bash会报的错误。也许你的shell不是bash。事实上,只要if单独存在,if本身不会出现任何错误:

$ if [somethingswrong]; then fail; fi # error, then `[` command must have space around it.
-bash: [somethingswrong]: command not found
你可以通过打印$SHELL来检查你的shell,也可以通过$BASH_VERSION来检查你的bash版本。(如果后者未设置,则你的shell不是bash。)

1
谢谢,我一直在使用c shell并在bashrc中进行更改。 - Chani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接