-1

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?

robertspierre
  • 3,218
  • 2
  • 31
  • 46
  • 2
    `In bash both of them are not equivalent to their numeric value:`?? In Bash a __string__ true and a __string__ false is just that - a string. – KamilCuk Jan 10 '23 at 17:17
  • 1
    You are passing arguments `==` and `0`/`1` to the `true`/`false` commands... it's not the shell reading those (I mean, technically it is, because those are builtins, but I mean, not the part that would handle actual shell syntax). So you don't have a comparison here other than what `if` does, which is "check if the result of the following command has a zero exit code". That's because in your Fish example, you didn't use the `[` command which you did use in Bash. – CherryDT Jan 10 '23 at 17:41
  • @KamilCuk Yes, `true` and `false` are strings, but they're also the names of (builtin) commands. The commands can be used directly (not in `[...]`) as conditions: `ok=true ; if $ok ; then echo OK ; else echo 'Huh?' ; fi` – Keith Thompson Jan 10 '23 at 18:17

1 Answers1

3

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
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • 2
    Once upon a time (version 2.7) the `fish` builtins `true` and `false` would raise an error if you gave them any arguments. Now (version 3.5) it just silently ignores them. (And in neither version is any mention of arguments documented.) I guess it's trying to be more POSIX compatible? – chepner Jan 10 '23 at 17:31
  • "I guess it's trying to be more POSIX compatible" -- more like it's changing to meet the expectations of users. – glenn jackman Jan 10 '23 at 21:37