function wtf() { echo "\$*='$*'" echo "\$@='$@'" echo "\$@='"$@"'" echo "\$@='""$@""'" if [ -n "$*" ]; then echo " [ -n \$* ]"; else echo "![ -n \$* ]"; fi if [ -z "$*" ]; then echo " [ -z \$* ]"; else echo "![ -z \$* ]"; fi if [ -n "$@" ]; then echo " [ -n \$@ ]"; else echo "![ -n \$@ ]"; fi if [ -z "$@" ]; then echo " [ -z \$@ ]"; else echo "![ -z \$@ ]"; fi } wtf
产生
06001
虽然在我看来[-n $@]应该是假的,因为7.3 Other Comparison Operators表示[-n“$X”]应该是所有$X的[-z“$X”]的倒数.
-z
string is null,that is,has zero length
06002
-n
string is not null.
The
-n
test requires that the string be quoted within the test brackets. Using an unquoted string with! -z
,or even just the unquoted string alone within test brackets (see Example 7-6) normally works,however,this is an unsafe practice. Always quote a tested string. [1]
我知道$@是特殊的,但我不知道它是否足以违反布尔否定.这里发生了什么?
$bash -version | head -1 GNU bash,version 4.2.42(2)-release (i386-apple-darwin12.2.0)
$[ -n "$@" ]; echo "$?" 0