I am learning the zsh in the conditional expressions part, and I found some unreasonable output.
For example:
$ mkdir emptyspace && cd emptyspace
$ var_assigned_with_emptystring=""
$ [[ -n $var_assigned_with_emptystring ]] && echo 'var_assigned_with_emptystring is a nonzero-len string!'
$ [[ -n "" ]] && echo '"" is a nonzero-len string!'
$ test -n $var_assigned_with_emptystring && echo 'var_assigned_with_emptystring is a nonzero-len string by test!'
var_assigned_with_emptystring is a nonzero-len string by test!
$ test -n "" && echo '"" is a nonzero-len string by test!'
$ [[ -z $var_assigned_with_emptystring ]] && echo 'var_assigned_with_emptystring is a zero-len string!'
var_assigned_with_emptystring is not an empty string!
$ [[ -z "" ]] && echo '"" is a zero-len string!'
"" is a zero-len string!
$ test -z $var_assigned_with_emptystring && echo 'var_assigned_with_emptystring is a zero-len string by test!'
var_assigned_with_emptystring is a zero-len string by test!
$ test -z "" && echo '"" is a zero-len string by test!'
"" is a zero-len string by test!
Why does the
test
with avar_assigned_with_emptystring
always gives a true result no matter it is with-n
or-z
option? What's the difference betweentest
command and individual conditional expression surrounded by[[]]
?What is the difference between
""
andvar_assigned_with_emptystring
? I think the parameter should have be expended(or evaluated) to a same value as""
when it comes to process in the internal part of test function. Buttest -n $var_assigned_with_emptystring
gives a different return status withtest -n ""