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

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



On Fri, Apr 10, 2015 at 4:05 AM, Kurtis Rader <krader@xxxxxxxxxxxxx> wrote:
> On Thu, Apr 9, 2015 at 7:02 PM, Kurtis Rader <krader@xxxxxxxxxxxxx> wrote:
>> On Thu, Apr 9, 2015 at 6:39 PM, Thorsten Kampe <thorsten@xxxxxxxxxxxxxxxx>
>> wrote:
>>
>>>
>>> ```
>>> VAR=
>>>
>>> if [[ $VAR ]]
>>> then
>>>     printf "something\n"
>>> else
>>>     printf "nothing\n"
>>> fi
>>> ```
>>>
>>> Works fine in zsh and bash.
>>>
>>> Same goes for
>>> `[[ $VAR ]] && printf "something\n" || printf "nothing\n"`
>>
>> Okay, I had forgotten that a bare string is equivalent to "-n string" in
>> bash. The zsh documentation makes no mention of this "feature". It probably
>> works for you and not me because of an option that differs between our two
>> environments. Although for the life of me I can't figure out what that
>> option is.
>
> Bart's reply explains why I get a parse error: Mac OS X still has zsh
> v5.0.5 which does not support the bash semantics for a bare string inside
> [[ ]]. Even if you don't need compatibility with pre v5.0.6 zsh releases I
> would discourage that syntax because of its ambiguity.

(please don't top post, it's a lot of work to fix the mail before replying)
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).

-- 
Mikael Magnusson



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