In my bash test has an attitude to exit with status 0:
$ test -n && echo true || echo false
-> true
while
$ test -n "" && echo true || echo false
-> false
It means when it doesn’t receive any argument at all, it assumes nonzero.
The case -z works properly instead:
$ test -z && echo true || echo false
-> true
$ test -z "" && echo true || echo false
-> true
Is this the expected behavior?
Basically, you are asking test whether the string “-z” is nonempty. It is, so it tells you
true. The actual algorithm test uses is:Quoted from the POSIX test command specification.