我.bashrc设置了一堆别名供我根据需要使用,然后自动运行其中一个。
事实证明,与使用交互式 shell 相比,这会导致自动脚本 ssh 进入我的机器时出现一些问题。所以,为了解决这个问题,我把它们放在一个 if 块中,这样它们就不会被定义或运行那些自动化脚本......
if [ -n "$TERM" ] && [ "$TERM" != "dumb" ] ; then
alias short='long command here'
alias another='very long command here'
# ...
short
fi
Run Code Online (Sandbox Code Playgroud)
只为看short: command not found!
让我们将其减少到最低限度......
$ cat alias.sh
alias foo='echo hi'
foo
Run Code Online (Sandbox Code Playgroud)
$ sh alias.sh
hi
Run Code Online (Sandbox Code Playgroud)
cat alias-in-if.sh
if true ; then
alias foo='echo hi'
foo
fi
Run Code Online (Sandbox Code Playgroud)
sh alias-in-if.sh
alias-in-if.sh: line 3: foo: command not found
Run Code Online (Sandbox Code Playgroud)
为什么第一个脚本有效,而不是第二个?
(我已经回答了我自己的问题。)