bashrc if:表达式语法错误

Cha*_*ani 5 unix bash

我写了以下.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
}
Run Code Online (Sandbox Code Playgroud)

但是,保存后,当我这样做时source .bashrc,我收到以下错误: if: Expression Syntax.

我究竟做错了什么 ?我用谷歌搜索了一段时间但没有用.

koj*_*iro 9

if: Expression Syntax 
Run Code Online (Sandbox Code Playgroud)

不是bash会给你的错误.也许你的shell不是bash.事实上,只要一个人if站立,任何错误都不会随之if而来:

$ if [somethingswrong]; then fail; fi # error, then `[` command must have space around it.
-bash: [somethingswrong]: command not found
Run Code Online (Sandbox Code Playgroud)

您可以通过回显来检查shell $SHELL,并且可以检查哪个版本的bash $BASH_VERSION.(如果后者未设置,则shell不是bash.)