Why [[ -n $var ]] instead of [[ $var ]]?

From the zsh 5.5.1 docs for CONDITIONAL EXPRESSIONS

For compatibility, if there is a single argument that is not syntactically significant, typically a variable, the condition is treated as a test for whether the expression expands as a string of non-zero length. In other words, [[ $var ]] is the same as [[ -n $var ]]. It is recommended that the second, explicit, form be used where possible.

With the source tree around,

% grep -rl 'if there is a single argument' .
./Doc/Zsh/cond.yo
% git blame ./Doc/Zsh/cond.yo | grep 'if there is a single argument'
d082827c83 (Jun T             2014-05-18 22:03:35 +0900 198) For compat...

Inspection of git log shows that the code change went in a bit earlier than the documentation:

commit 9d47e8398d299e53ffe4e7ddf3731d2fedae9948
...
Date:   Tue May 13 08:16:50 2014 -0700

    32609: [[ $var ]] behaves as [[ -n $var ]] for bash/ksh compatibility

The mapping of the ChangeLog file to git tag is not clear to me, but it appears zsh 5.0.6 (Thu Aug 28 19:07:04 2014 +0100) is the first version with this change.

Tags:

Zsh