From: Stephane Chazelas <stephane.chazelas@gmail.com>
To: zsh-workers@zsh.org
Subject: Re: `[[ -n $VAR ]]` equal to `[[ $VAR ]]`?
Date: Fri, 10 Apr 2015 21:33:05 +0100 [thread overview]
Message-ID: <20150410203305.GA2727@chaz.gmail.com> (raw)
In-Reply-To: <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
parent reply other threads:[~2015-04-10 20:37 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <CABx2=D_4Aw4W9AsBNa5o1houzAipLFDBtt82BnVq0CEyNshSUA__23909.3113611036$1428636185$gmane$org@mail.gmail.com>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150410203305.GA2727@chaz.gmail.com \
--to=stephane.chazelas@gmail.com \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).