Why are the bash -n and -z test operators not inverses for $@ Why are the bash -n and -z test operators not inverses for $@ bash bash

Why are the bash -n and -z test operators not inverses for $@


When $@ is empty, "$@" doesn't expand to an empty string; it is removed altogether. So your test is not

[ -n "" ]

but rather

[ -n ]

Now -n isn't an operator, but just a non-empty string, which always tests as true.


"$@" doesn't do what you expect. It's not a different form of "$*", it expands to the quoted list of arguments passed to the current script.

If there are no arguments, it expands to nothing. If there are two arguments a and b c, then it expands to "a" "b c" (i.e. it preserves whitespace in arguments) while "$*" expands to "a b c" and $* would expand to a b c (three words).