为什么'test -n'在bash中返回'true'? [英] Why does 'test -n' return 'true' in bash?

查看:40
本文介绍了为什么'test -n'在bash中返回'true'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道怎么回事

test -n

返回"true",例如:

return 'true', for example :

if test -n; then echo "yes" ; else echo "no" ; fi

即使在理论上进行了测试,

也会打印是",从理论上讲,空长度的字符串作为参数与选项-n一起使用,该选项检查字符串的长度是0(返回false)还是其他值(返回true)

prints "yes", even though test was given, theoretically, an empty-length string as an argument along with the option -n, which checks whether the string length is 0 (returns false) or something else (returns true).

谢谢

推荐答案

出于相同的原因,它返回true. test x 返回true-字符串 -n 不是-空的.它没有执行 -n 选项,因为 -n 需要第二个参数,而您没有提供该参数.

It returns true for the same reason test x returns true - the string -n is non-empty. It is not exercising the -n option because -n requires a second argument and you've not provided one.

test -n  ""  || echo false
x=""
test -n  $x  && echo true
test -n "$x" || echo false

每个echo命令都被执行;注意,尤其是中间一个!

Each echo command is executed; note, in particular, the middle one!

这篇关于为什么'test -n'在bash中返回'true'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆