为什么 $((true == false)) 在 bash 中评估为 1?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8579399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 01:12:05  来源:igfitidea点击:

Why does $((true == false)) evaluate to 1 in bash?

bash

提问by HaskellElephant

Why does bash have the following behavior?

为什么 bash 有以下行为?

echo $((true == false))
1

I would have thought that this would print 0, but it prints 1.

我原以为这会打印0,但它会打印1

This is further complicated by the following facts:

以下事实使情况变得更加复杂:

> echo $((true))
0
> echo $((false))
0
> echo $((true == true))
1
> echo $((false == false))
1

回答by zostay

All the posters talking about 0 being true and 1 being false have missed the point. In this case, 1 is true and 0 is false in the usual boolean sense because of the arithmetic evaluation context caused by $(()).

所有谈论0为真,1为假的海报都没有抓住重点。在这种情况下,由于由 引起的算术评估上下文,在通常的布尔意义上,1 为真,0 为假$(())

The ==operation inside of $(())is not equality of return statuses in Bash, it performs numeric equality using the literals given where "false" and "true" are treated as variable, but have not yet been bound, which both are interpreted as 0 since they have no value yet assigned:

==里面的操作$(())不是 Bash 中返回状态的相等,它使用给定的文字执行数字相等,其中“false”和“true”被视为变量,但尚未绑定,因为它们都被解释为 0尚未分配值:

$ echo $((true))
0
$ echo $((false))
0

If you want to compare the return status of true and false you want something like:

如果要比较 true 和 false 的返回状态,则需要以下内容:

true
TRUE=$?
false
FALSE=$?
if (( $TRUE == $FALSE )); then echo TRUE; else echo FALSE; fi

But, I'm not sure why you would want to do this.

但是,我不确定您为什么要这样做。

EDIT:Corrected the part in the original answer about "true" and "false" being interpreted as strings. They are not. They are treated as variables, but have no value bound to them yet.

编辑:更正了原始答案中关于“true”和“false”被解释为字符串的部分。他们不是。它们被视为变量,但还没有绑定到它们的值。

回答by fge

OK, so, it seems there is a lot of confusion about what $((...))really does.

好的,所以,似乎对$((...))真正的作用有很多困惑。

It does an arithmetic evaluation of its operands, barewords are variables, not commands or string literals(ie, trueis really $true), and anything which is not a number is 0. The ==operator compares two numbers, and returns 1 if they are equal.

对其操作数进行算术评估,裸字是变量,而不是命令或字符串文字(即trueis real $true),并且任何不是数字的东西都是 0。该==运算符比较两个数字,如果相等则返回 1。

Which is why $((true == false))is 1: there is no trueor falseenvironment variables in the environment, which means both $trueand $falseevaluate to the empty string, therefore 0 in arithmetic context!

这就是为什么$((true == false))是 1:环境中没有trueorfalse环境变量,这意味着两者$true$false评估为空字符串,因此在算术上下文中为 0!

To be complete, you can also use command substitution in arithmetic context... For instance:

为了完整起见,您还可以在算术上下文中使用命令替换......例如:

$ echo $((`echo 2`))
2
$ echo $((3 + $(echo 4)))
7
$ a=3
$ echo $((a + $(echo 4)))
7
# undefine a
$ a=
$ echo $((a + $(echo 4)))
4
$ a="Hello world"
$ echo $((a + 1))
1
# undefine a again
$ a=
$ echo $(($(echo $a)))
0

回答by user1108379

Because in bash, 0 is true and everything other than 0 is false.

因为在 bash 中,0 为真,除 0 外的所有内容均为假。