Description of problem: test -n doesn't seem to work. It is stated in the man that "STRING equivalent to -n STRING", however, the following script prints "14", which is impossible. --- if test -n "$@"; then echo -n 1 else echo -n 2 fi if test "$@"; then echo -n 3 else echo -n 4 fi echo --- Version-Release number of selected component (if applicable): coreutils-6.9-3.fc7 How reproducible: always Steps to Reproduce: 1. Run the attached script Actual results: 14 Expected results: 24 Additional info: Well, I know that such a bugs are very unlikely, and I might be missing something obvious. Or maybe it is just a documentation error, and -n means something very different...
Created attachment 232901 [details] the test case
Are you invoking test(1) or bash's built-in test command?
test(1) from coreutils. And using explicitly /usr/bin/test gives the same, and [ ] too.
You are supplying more than one parameter. "$@" expands to one word per argument, i.e. $# words. Perhaps one of those words is a test option. Looks like you probably want "$*" there.
> You are supplying more than one parameter. Where? I run the script without any parameters. > "$@" expands to one word per argument, i.e. $# words. OKey, echo $# from that script gives 0. And echo -n "$@" >a gives a zero-sized file. > Perhaps one of those words is a test option. Have you tried starting the script? I am not trying to cheat you by supplying one of the "test" options as a parameter to the script - that would be a waste of time. Instead, I made sure "$@" is empty by echoing it into a file. > Looks like you probably want "$*" there. My point is that test "$@" and test -n "$@" gives different results, while the man states they are the same. This is clearly demonstrated by my script. Can you explain that difference?
What I'm saying is that "$@" is special. In the case where there are no arguments supplied, it is 0 words, i.e. it is replaced by absolutely nothing, not even an empty string. So 'test "$@"' is not testing a string, it is testing *nothing*, so is trivially true. Not a bug -- use "$*".
But that does not answer my question why 'test -n "$@"' and 'test "$@"' give the different results. If it is not the bug, then at least the documentation is wrong, because it says its the same.
> it is replaced by absolutely nothing, > not even an empty string. Aren't the " around the nothing makes it an empty string btw? > So 'test "$@"' is not testing a string, > it is testing *nothing*, so is trivially > true. No, 'test "$@"' is exactly false, but 'test -n "$@"' is true.
(In reply to comment #8) > > it is replaced by absolutely nothing, > > not even an empty string. > Aren't the " around the nothing makes > it an empty string btw? No. "$@" is special-cased in bash. If there are no parameters then it evaluates to nothing, not even an empty string.
> No. "$@" is special-cased in bash. Ah, ok then. And 'test' without any parameters returns false, right? Thank you both for the explanations.