Consider, below code works as expected:
if [[ $SOME_VARIABLE = "TRUE" ]]; then
echo "Only echoed when \$SOME_VARIABLE stores string \"TRUE\"."
fi
But when I remove the space surrounding the equality operator it always evaluates to 0 exit status (At least that’s what I assume it must be returning as it is taken as true):
if [[ $SOME_VARIABLE="TRUE" ]]; then
echo "Always true."
fi
UPDATE:
Just to confirm whether the issue lies with the equality operator or not:
#!usr/bin/ksh
SOME_VARIABLE=FALSE
if [[ $SOME_VARIABLE == "TRUE" ]]; then
echo "Only echoed when \$SOME_VARIABLE stores string \"TRUE\"."
fi
if [[ $SOME_VARIABLE=="TRUE" ]]; then
echo "Always true."
fi
[kent@TEST]$ sh test.sh
Always true.
UPDATE:
Summary:
- Using
=is the same as==above, but is obsolete. - ALWAYS mind your spaces.
From
ksh(1):So the following expression is true:
Now consider your second example:
Assuming
$SOME_VARIABLEis “SOMETHINGNOTTRUE”, this expands to:“SOMETHINGNOTTRUE=TRUE” is a non-zero length string. It is therefore true.
If you want to use operators inside of
[[, you must put spaces around them as given in the docs (note the spaces):