为什么都是"真"和"假"测试真的吗? [英] Why are both "true" and "false" tests true?

查看:304
本文介绍了为什么都是"真"和"假"测试真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字真与假是bash的特殊字词(内建)。
如果在如果测试使用,它们充当直观地预期:

The words "true" and "false" are special words (builtins) for bash. If used in an if test, they act as intuitively expected:

$ if true; then echo "true"; else echo "false"; fi
true
$ if false; then echo "true"; else echo "false"; fi
false

不过,这两项测试:

However, this two tests:

$ [[ true ]] && echo "true" || echo "false"
true
$ [[ false ]] && echo "true" || echo "false"
true

这两种结果都是如此。为什么呢?

Both result in true. Why?

推荐答案

[...] ,在这种情况下,等同于测试,即测试真实试验假。纵观手动测试(1):

[[ … ]] is, in this case, equivalent to test, i.e. test true and test false. Looking at the manual for test(1):

-n STRING 字符串的长度为非零

字符串等同于 -n STRING

真正都是非空字符串。

这篇关于为什么都是"真"和"假"测试真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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