How does Bash determine true/false?
For example:
if true; then echo "true!"; else echo "false"; fi
if false; then echo "true!"; else echo "false"; fi
# Negation works here too.
if ! true; then echo "true!"; else echo "false"; fi
# Is 47 an odd number?
if perl -e 'my $bool = shift() % 2; exit ! $bool' 47; then
echo "That's an odd number"
else
echo "That's an even number"
fi
[ "foobar" = "qux" ]; echo $? # A return value of 0 means true, 1 means false. This is false. [ "$foo" ]; echo $? # True if $foo evaluates to the empty string. [ 1 ]; echo $? # True. [ 0 ]; echo $? # True because "0" is a non-empty string. [ false ]; echo $? # True because this is interpreted as the string "false", not a command to be run. [ "" ]; echo $? # False. [ ]; echo $? # False.
# Are we currently running under Cygwin?
if [[ "$(uname)" =~ ^CYGWIN_ ]]; then
## ^^ Don't surround this with quotes, otherwise it will be interpreted as a literal string instead of a regex.