%20return%20values%20do%20you%20need%20for%20if%2C%20while%20and%20until%20to%20signal%20truthiness%20or%20falsiness%3F.png)
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"