The $ sign introduces parameter expansion, command substitution, or arithmetic expansion
In your second case ($var
unquoted) since $var
is empty, it will expand to nothing. Just as if you had not specified any operand to the test.
In your first case, ($var
double quoted) $var
will still expand to nothing but within the double quotes, this incidentally providing the empty string ""
as operand to the test.
Update following additional question from OP in comments :
why doesn't -n with no operand lead to, for example, "error: -n
requires an operand"?
To which I had initially answered that, according to the manual, test
Exits with a status of 0 (true) or 1 (false)
With false including the case where "an invalid argument is given".
Some comment from illkachu provides a deeper insight :
A few test implementations (including Bash's) actually do return a
distinct falsy status for an error, i.e. 0 for true, 1 for false and 2
for error
However :
It's just that both 1 and 2 are falsy as far as shell conditionals are
concerned
[ -n ]
be relevant when they clearly had[ -n $var ]
? Why would[ -n ]
be true? What does it even mean? – ilkkachu Dec 28 '23 at 12:09"$var"
will only ever result in one argument (or word/field, howevery you like), never none (nor more than one) – ilkkachu Dec 28 '23 at 12:17