在我的 bash 中 test
有退出状态 0
的态度:
$ test -n && echo true || echo false
-> true
同时
$ test -n "" && echo true || echo false
-> false
这意味着当它根本没有收到任何参数时,它假定为非零值。
情况 -z
可以正常工作:
$ test -z && echo true || echo false
-> true
$ test -z "" && echo true || echo false
-> true
这是预期的行为吗?
最佳答案
基本上,您是在询问测试字符串“-z”是否为非空。它是,所以它告诉您 true
。实际算法测试使用的是:
0 arguments:
Exit false (1).
1 argument:
Exit true (0) if $1 is not null; otherwise, exit false.
2 arguments:
If $1 is '!', exit true if $2 is null, false if $2 is not null.
If $1 is a unary primary, exit true if the unary test is true, false if the unary test is false.
Otherwise, produce unspecified results.
...
关于linux - BASH:[](测试)行为不一致,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7890017/