The comparison isn't doing whatever it is you think it's doing, and the two statements are entirely dissimilar. Because of the way Bash parses such things:
set -x
var1="false"
if "$var1"; then
echo "This branch never executes."
fi
var1="true"
if "$var1"; then echo $?; fi
set +x
shows that this minor adaptation of your code is actually running the true
or false
binary in your PATH (probably /bin/false
or /bin/true
in most cases) and returning the exit status from the binary. Likewise, [[ "true" ]]
and [[ "false" ]]
always exit zero because this construct creates in implicit -n
test (e.g. variable exists and is not empty), so you aren't doing any comparisons where there's a failing condition since both var1 and var2 are defined.
What you probably want is string equality. For example:
set -x
var1="false"
var2="true"
if [[ "$var1" = "true" ]] || [[ "$var2" = "true" ]]
then
echo "At least one of the two values is true."
fi
set +x
This performs a string comparison between the values, and returns an appropriate exit status with a Boolean short-circuit.