Is there any difference between
if [ ! -z "$var" ] then
# do smth
fi
and
if [ "$var" ] then
# do smth
fi
They both seem to check if variable is set
Is there any difference between
if [ ! -z "$var" ] then
# do smth
fi
and
if [ "$var" ] then
# do smth
fi
They both seem to check if variable is set
Yes, they're equivalent, but there are a couple of notes that apply to both of them:
]
and the then
keyword, or it'll misparse them weirdly.However, I actually prefer a third also-equivalent option:
if [ -n "$var" ]; then
I consider this semantically clearer, because the -n
operator specifically checks for something being non-empty.
There's no difference between
[ ! -z "$var" ]
[ -n "$var" ]
[ "$var" ]
All of them are true if $var
is not empty.