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

Solution 1:

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 $(()).

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:

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

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

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.

Solution 2:

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, true is really $true), and anything which is not a number is 0. The == operator compares two numbers, and returns 1 if they are equal.

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

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