When I run this code in Solaris bash:
a=false;
b=false;
if [ $a -o $b ] ; then
echo "_True"
else
echo "_False"
fi
result:
_True
Shouldn't the output of the script be false?
If I modify the script to something like this:
a=false;
b=false;
if [ $a = true -o $b = true ] ; then
echo "_True"
else
echo "_False"
fi
result:
_False
but this doesn't feel like a good coding practice to write "$a = true".
Please, can i know what is the issue here? and what exactly is the solution?
The [
command treats all non-empty strings as true. So use an empty string to represent false, rather than the string "false"
. And remember to quote the variables.
a=
b=
if [ "$a" -o "$b" ]; then
echo "_True"
else
echo "_False"
fi
Another option is to use the true
and false
commands, rather than [
.
a=false
b=false
if $a || $b; then
echo "_True"
else
echo "_False"
fi