What kind of (different) return values do you need for if, while and until to signal truthiness or falsiness?

What kind of (different) return values do you need for if, while and until to signal truthiness or falsiness?

I am curious about the expected value of conditionals in if, while and until statements, because I can't obviously not seem to get my conditionals working properly.

Do they expect true and false or an exit code, i.e. 0 or {1-255} ?

Antwort1

The conditionals test for an exit code. Zero is true, and non-zero is false. The true command is true by virtue of it returning a zero (0) exit code. Similarly, false returns a non-zero exit code (as it happens, it's 1).

if true; then echo 'happy days'; fi
if false; then echo 'maths has just died a horrible death'; fi

true; echo "exit status is $?"     # "0"
false; echo "exit status is $?"    # "1"

[ 0 -eq 0 ]; echo "exit status is $?"    # "0"
[ 0 -eq 1 ]; echo "exit status is $?"    # "1"

verwandte Informationen