Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
Re: `[[ -n $VAR ]]` equal to `[[ $VAR ]]`?
- X-seq: zsh-users 20126
- From: Kurtis Rader <krader@xxxxxxxxxxxxx>
- To: Mikael Magnusson <mikachu@xxxxxxxxx>
- Subject: Re: `[[ -n $VAR ]]` equal to `[[ $VAR ]]`?
- Date: Thu, 9 Apr 2015 20:21:50 -0700
- Cc: Thorsten Kampe <thorsten@xxxxxxxxxxxxxxxx>, Zsh Users <zsh-users@xxxxxxx>
- In-reply-to: <CAHYJk3Qj6W=VZ26si0VjhsmS1u9go6mVGpXjDSTLk5knKaEtOg@mail.gmail.com>
- List-help: <mailto:zsh-users-help@zsh.org>
- List-id: Zsh Users List <zsh-users.zsh.org>
- List-post: <mailto:zsh-users@zsh.org>
- Mailing-list: contact zsh-users-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>
On Thu, Apr 9, 2015 at 7:46 PM, Mikael Magnusson <mikachu@xxxxxxxxx> wrote:
> If you mean ambiguity because VAR might be the string "-z" for
> example, there is no such ambiguity. The syntax of [[ ... ]] is fully
> parsed before any substitutions inside are made (I think, but it's at
> least true for this purpose). (This is unlike [ ... ] which is not
> syntax but a shell builtin that gets passed arguments after they're
> substituted, globbed, expanded and etc).
>
Your last statement is only partially true. It is true that most modern
shells implement the single bracket form as a builtin (the double bracket
form has always been a builtin in ksh, zsh, etc.). However, historically
the "[" command was simply an alias (usually a file system hard-link) for
/bin/test. The original Bourne shell simply executed that external command
and checked its exit status. Check your /bin or /usr/bin directory and
you'll likely see a file named "[" (although it may not be a link to
/bin/test these days). The fact the "[" was historically an external
command is why you have to jump through hoops to quote variable expansions
and use tricks like
if [ "${var}x" = x ]; then
so that if $var was unset or the empty string you still had a valid
expression.
--
Kurtis Rader
Caretaker of the exceptional canines Junior and Hank
Messages sorted by:
Reverse Date,
Date,
Thread,
Author