Categories

请点击这里

What should be noticed while using -n and -z Bash expression

[ -z $string ] is very easy to use. We use this expression to test whether the string is empty or undefined.
But I did meet problem when use [ -n $string ]. It seemed that it returned true anyway, no matter the $string is empty or defined or not-defined.
But when I double quoted the variable, it worked. So if we want anticipated result, we need to write it in this way: [ -n "$string" ].
But for “-z”, they are all the same, no matter you write [ -z $string ] or [ -z "$string" ].

Anybody knows why?

Leave a Reply