Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: `[[ -n $VAR ]]` equal to `[[ $VAR ]]`?



2015-04-09 20:21:50 -0700, Kurtis Rader:
[...]
> if [ "${var}x" = x ]; then
> 
> so that if $var was unset or the empty string you still had a valid
> expression.
[...]

No, that "x" (and you need it *before* $var) was never about empty $var, 


if [ "$var" = "" ]

is perfectly fine in the Bourne shell when $var is empty.

The problem is with values of $var like ! or (.

That's for those that you need:

if [ "x$var" = x ]
or
if [ "" = "$var" ]

IIRC
if [ -n "$var" ]

would be a problem for values of $var like = in some shell.
Still is with /bin/sh on Solaris 10.

I don't know if any shell ever had a problem with

if [ "${var}x" = x ]

when $var == "-". I wouldn't bet on it.


Now POSIX clearly specifies "[" aka test when the number of
arguments is less than 4, so one can count on

[ -n "$var" ]

and

[ "$var" = "" ]

being reliable in conformant shells/tests.

More info at:

http://www.in-ulm.de/~mascheck/various/test/

-- 
Stephane



Messages sorted by: Reverse Date, Date, Thread, Author