Reputation: 1104
I'm trying to understand why an unset variable gets evaluated as 0. in some scripts im writing the variable will be set only if needed and some time it does not. so this kind of behave will result with incorrect output. does it mean i must preset all my variables or at least add check that they are set ?
#!/bin/bash
#myvalue=0 #comment to simulate an unset variable.
if [[ $myvalue -eq 0 ]] ; then
echo "OK"
fi
result with OK:
bash -x test.sh
+ [[ '' -eq 0 ]]
+ echo OK
OK
Upvotes: 0
Views: 854
Reputation: 47159
If you want the literal value to be the comparison use =
instead of -eq
.
if [[ $myvalue = 0 ]] ; then
echo "OK"
fi
The arithmetic binary operator (-eq
) returns true if arg1 is equal to 0
, which $myvalue
is, whether set to 0
or not set at all... ''
is null, which equals zero.
Upvotes: 0
Reputation: 530920
The -eq
operator inside [[ ... ]]
, since it only applies to integer values, triggers arithmetic evaluation of its operands. In an arithmetic expression, unset variables default to 0. A more obvious demonstration of arithmetic evaluation:
$ if [[ 3 -eq "1 + 2" ]]; then echo equal; fi
equal
Note that in your example, you don't even need to expand the parameter first; the arithmetic evaluation will do it for you:
$ if [[ myvalue -eq 0 ]]; then echo equal; fi
equal
$ myvalue=3
$ if [[ myvalue -eq 3 ]]; then echo equal; fi
equal
Also, this is specific to the bash
[[ ... ]]
command. With POSIX [
, -eq
does not trigger arithmetic evaluation.
$ if [ "$myvalue" -eq 0 ]; then echo equal; fi
bash: [: : integer expression expected
$ if [ myvalue -eq 0 ]; then echo equal; fi
bash: [: myvalue: integer expression expected
Upvotes: 3