From the man test
pages:
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
Executing [ -n $foo ]
, [ -n ]
, [ -z $foo ]
, [ -z ]
, all return true. Is this just sloppy return value? An unset variable is not a STRING. The total omission of an expression is not either. And even if it WERE being somehow "coerced" into one, I'm disinclined to think it's length could be both zero and non-zero simultaneously.
Indeed, both
if [ -n ] && [ -z ]; then
echo 1;
fi
# AND
if [ -n $foo ] && [ -z $foo ]; then
echo 1;
fi
...both yield "1"
. Why? What's going on under the hood here?