Home > database >  In fish true == 0, but false != 1?
In fish true == 0, but false != 1?

Time:01-12

In fish, true seems to equal 0:

❯ if true == 0; echo "YES"; else; echo "NO"; end
YES

But false seems to not equal 1:

❯ if false == 1; echo "YES"; else; echo "NO"; end
NO

In bash both of them are not equivalent to their numeric value:

$ if [ true == 0 ]; then echo "YES"; else echo "NO"; fi
NO
$ if [ false == 1 ]; then echo "YES"; else echo "NO"; fi
NO

It seems strange that fish would be consider one truth value equal to its numeric counterpart but not the other.

Maybe there is an explanation for that?

CodePudding user response:

The command true ignores arguments and exits with zero exit status meaning success.

if true you can put literally anything here; echo "YES"; else; echo "NO"; end

The command false ignores arguments and exits with non-zero exit status which means failure.

if false you can put literally anything here too; echo "YES"; else; echo "NO"; end

The equivalent Bash code is:

if true anything here; then echo "YES"; else echo "NO"; fi
if false anything here too; then echo "YES"; else echo "NO"; fi

The command [ true == 0 ] executes the command [ and compares the string true to the string 0. Because true and 0 are different strings, the comparison is (logically) false, so [ command exits with non-zero exit status. Similarly with false == 0. Note that == is an extension to [ - it's [ true = 0 ] in standard [.

You can compare that in Fish the [ command also "both of them are not equivalent to their numeric value" too:

if [ true == 0 ]; echo "YES"; else; echo "NO"; end
if [ false == 1 ]; echo "YES"; else; echo "NO"; end
  • Related