From man test
:
! EXPRESSION
EXPRESSION is false
and
-n STRING
the length of STRING is nonzero
STRING equivalent to -n STRING
-z STRING
the length of STRING is zero
So [ "$x" ]
has the same effect as [ -n "$x" ]
(that's explicitly stated above) and implicitly, [ ! "$x" ]
has the same effect as [ -z "$x" ]
. So why would one choose to use -n
or -z
when arguably the other forms are clearer? Is it just personal preference or am I missing an important distinction?
I've seen other questions (e.g. this one) about this but nothing to say for definite if they're functionally identical. I did wonder whether I could avoid quoting $x
if I used -n
or -z
but if $x
contains a space, this fails as with the other methods.