bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: "[ -n ${emptyvariable} ]" returns success


From: Paul Jarc
Subject: Re: "[ -n ${emptyvariable} ]" returns success
Date: Thu, 20 Sep 2007 19:46:40 -0400
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux)

address@hidden wrote:
>       foo=
>       [ -n ${foo} ] && echo true
>
>       Prints "true".

That's the correct behavior.

>       Should probably print an error, since after the command is
>       expanded there are only three arguments, so the final ] should
>       be interpreted as the argument to -n, thus leaving the command
>       with a missing ] terminator.

No, the final ] is always taken to be the match for [.  Anything in
between has to be parsed consistently with that.  In this case, it
means that the test expression includes only one argument, "-n".
Since there is only one, it is taken to be an operand, even if it
happens to have the same spelling as an operator.  The operand is
tested for being nonempty, which "-n" is, so the result here is true.

To avoid pitfalls like this, always quote variable expansions.  The [[
command is also less tricky.


paul




reply via email to

[Prev in Thread] Current Thread [Next in Thread]