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

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

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

Kurtis Rader
Caretaker of the exceptional canines Junior and Hank

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