bash bashrc if: 表达式语法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14440105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:19:00  来源:igfitidea点击:

bashrc if: Expression Syntax error

bashunix

提问by Chani

I have written the following .bashrc :

我写了以下 .bashrc :

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific aliases and functions


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

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

However, after saving, when I did source .bashrc, i got the following error: if: Expression Syntax.

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

What am I doing wrong ? I googled a while but no avail.

我究竟做错了什么 ?我用谷歌搜索了一段时间,但无济于事。

回答by kojiro

if: Expression Syntax 

is not an error bash would give you. Perhaps your shell is not bash. In fact, as long as ifstands alone, any error would not be with ifitself:

不是 bash 会给你的错误。也许你的 shell 不是 bash。事实上,只要if单独存在,任何错误都不会发生在if自己身上:

$ if [somethingswrong]; then fail; fi # error, then `[` command must have space around it.
-bash: [somethingswrong]: command not found

You can check your shell by echoing $SHELL, and you can check which version of bash with $BASH_VERSION. (If the latter is unset, your shell is not bash.)

你可以通过 echo 来检查你的 shell $SHELL,你可以用$BASH_VERSION. (如果后者未设置,则您的 shell 不是 bash。)