Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: `[[ -n $VAR ]]` equal to `[[ $VAR ]]`?
- X-seq: zsh-workers 34873
- From: Stephane Chazelas <stephane.chazelas@xxxxxxxxx>
- To: zsh-workers@xxxxxxx
- Subject: Re: `[[ -n $VAR ]]` equal to `[[ $VAR ]]`?
- Date: Fri, 10 Apr 2015 21:33:05 +0100
- In-reply-to: <CABx2=D_4Aw4W9AsBNa5o1houzAipLFDBtt82BnVq0CEyNshSUA__23909.3113611036$1428636185$gmane$org@mail.gmail.com>
- List-help: <mailto:zsh-workers-help@zsh.org>
- List-id: Zsh Workers List <zsh-workers.zsh.org>
- List-post: <mailto:zsh-workers@zsh.org>
- Mailing-list: contact zsh-workers-help@xxxxxxx; run by ezmlm
- References: <mg78mg$vqb$1@ger.gmane.org> <CABx2=D-VTpp3ihAE_A2KCdE3tcPu5RPuB8WJwtv-G=B0QNx0GQ@mail.gmail.com> <mg79kr$vqb$2@ger.gmane.org> <CABx2=D9Mk01ovQkMyWT_1nh9WdDvcpqmMJAOdka-oEmN8Xe4nw@mail.gmail.com> <CABx2=D93rJDoqGHa=9=nGZZU=9oC_vc-0FHE8ZERV9LmxtqRaA@mail.gmail.com> <CAHYJk3Qj6W=VZ26si0VjhsmS1u9go6mVGpXjDSTLk5knKaEtOg@mail.gmail.com> <CABx2=D_4Aw4W9AsBNa5o1houzAipLFDBtt82BnVq0CEyNshSUA__23909.3113611036$1428636185$gmane$org@mail.gmail.com>
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