失败条件的退出状态是什么?

What's the exit status of a failed condition?

[ $var -eq $val ]

上面的condition/test如果失败了,$?的值是多少?我可以假设它 总是 1 吗?

编辑:阅读答案后,我意识到我的问题并不准确。我的意思是 "will it be always 1 if no error occurs?".

没有。如果条件失败,它不会总是 1。

例如:

[root@localhost ~]# [ xxxxxx15 -gt "" ]
bash: [: xxxxxx15: integer expression expected
[root@localhost ~]# echo $?
2

退出状态可能会根据您申请的operator/conditions而有所不同

是的,至少只要不发生错误(我猜在一般情况下技术上 "no"?)。见 http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_14:

The following exit values shall be returned:

  • 0

    expression evaluated to true.

  • 1

    expression evaluated to false or expression was missing.

  • >1

    An error occurred.

https://www.gnu.org/software/bash/manual/bashref.html#Bourne-Shell-Builtins:

Evaluate a conditional expression expr and return a status of 0 (true) or 1 (false).