为什么 if 语句在 ` if [ ''=1 ] 中执行;然后回显“真”;否则回声“假”;菲`?

显示键
if test-commands; then
  consequent-commands;
[elif more-test-commands; then
  more-consequents;]
[else alternate-consequents;]
fi

执行 test-commands 列表,如果其返回状态为零,则执行 consequent-commands 列表。

''=1在 bash 中显示状态

''=1
bash: =1: command not found
echo $?
127

status 的值为 127,而不是零。奇怪的说法:

if [ ''=1 ] ; then echo "true"; else echo "false"; fi
true

为什么状态值127,而不是零调用 then 语句?为什么进不去falsebash?

@德米特里沙托夫

=1
bash: =1: command not found
echo $?
127
巴乔利克

你需要空格

''=1被解释为一个字符串=1并且test找到一个非空字符串。如果你想比较''1,写

[ '' = 1 ]

为什么是状态 127?

man bash

... 如果该函数未定义,shell 会打印一条错误消息并返回退出状态 127。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章