To what value does an unquoted, undeclared variable expand to in order to return an exit status of 0?
Here is an example of a particular situation I ran into:
[ -n $var ]; echo $?
0
[ -n "$var" ]; echo $?
1
In both tests, the variable var is not declared.
I could have saved me the hassle by testing with -z, where quoted or unquoted apparently doesn't make a difference, but I ran into this particular situation and I started wondering. I looked deeper into all the expansions that bash performs, but couldn't find any explanation for this behavior.
As a general rule I usually quote variables, but hopefully the reason of this behavior helps me better understand quoting.