为什么我在 ~/.profile 中收到 if: Expression 语法错误?

Why am I getting an if: Expression syntax error in ~/.profile?

当我尝试 source ~/.profile 时,我的 .profile 出现 'if: Expression syntax' 错误。我究竟做错了什么? .profile file

来自 link 的代码在我的 bash 环境中看起来 'ok'、运行s 'ok',并检出为 'ok' @ShellCheck.

根据 OP 的评论,以下 不会 产生语法错误:

$ bash
$ source ./.profile
$                         <<<=== no error message, just a command prompt

这证实了 OP 的 .profile 的内容对于 bash 环境是 'ok'。

我们从其他评论中得知:

$ echo $SHELL
/bin/tcsh

问题就出在这里……tcshbash不一样,这也意味着OP的.profile中的一些命令在[下执行时无效=17=] shell.

如果我们去 Try It Online,我们可以确认这一点,向下滚动并选择 tcsh,输入以下代码 ...

tty -s
if test $? = 0
then
    stty dec crt
fi

... 当我们 运行 我们得到代码时:

if: Expression Syntax.

要解决这个语法问题,有几个选项:

  • OP 可以将默认 shell 切换为 bash(请参阅 chsh 或让系统管理员更新 /etc/passwd 以替换 tcsh shell 与 bash shell);一旦完成,所有新的 login/shell 会话应该没有问题采购 OP 的 .profile
  • 或...
  • OP 可以在命令行手动 运行 bash 然后手动获取 .profile