如果运算符周围有空格,则test
返回表达式的正确值。
但是如果没有空格,它不会抛出任何语法错误并且总是返回 true。
$ test "A" == "A"; echo $?
0 #<-- OK, 0 stands for true
$ test "A" == "B"; echo $?
1 #<-- OK, 1 stands for false
$ test "A"=="A"; echo $?
0 #<-- OK, 0 stands for true
$ test "A"=="B"; echo $?
0 #<-- ??? should either return 1 (false), or throw a syntax error (exit code > 1)
那是因为语法没有错:
test "A"=="B"
与 相同test foo
,它正在测试一个字符串,并且由于该字符串不为空,因此它返回 true。这在以下test
部分中进行了解释man bash
:参数由空格定义,因此由于 周围没有空格,因此
==
整个字符串"A"=="B"
被解析为单个参数。它在行动中:
正如您在上面看到的,传递一个空字符串将返回 false,但传递一个非空字符串返回 true。