Bug 340291

Summary: test -n doesnt work
Product: [Fedora] Fedora Reporter: Stas Sergeev <stsp>
Component: coreutilsAssignee: Pete Graner <pgraner>
Status: CLOSED NOTABUG QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: low Docs Contact:
Priority: low    
Version: 7CC: ivazqueznet, twaugh
Target Milestone: ---   
Target Release: ---   
Hardware: All   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: Bug Fix
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2007-10-22 04:26:17 EDT Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: ---
Attachments:
Description Flags
the test case none

Description Stas Sergeev 2007-10-19 14:27:08 EDT
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...
Comment 1 Stas Sergeev 2007-10-19 14:27:08 EDT
Created attachment 232901 [details]
the test case
Comment 2 Ignacio Vazquez-Abrams 2007-10-19 14:34:34 EDT
Are you invoking test(1) or bash's built-in test command?
Comment 3 Stas Sergeev 2007-10-19 14:51:26 EDT
test(1) from coreutils.
And using explicitly /usr/bin/test gives
the same, and [ ] too.
Comment 4 Tim Waugh 2007-10-22 04:26:17 EDT
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.
Comment 5 Stas Sergeev 2007-10-22 14:31:17 EDT
> 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?
Comment 6 Tim Waugh 2007-10-22 16:16:50 EDT
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 "$*".
Comment 7 Stas Sergeev 2007-10-22 23:53:37 EDT
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.
Comment 8 Stas Sergeev 2007-10-23 00:01:00 EDT
> 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.
Comment 9 Ignacio Vazquez-Abrams 2007-10-23 00:32:34 EDT
(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.
Comment 10 Stas Sergeev 2007-10-23 00:54:50 EDT
> No. "$@" is special-cased in bash.
Ah, ok then. And 'test' without any parameters
returns false, right?
Thank you both for the explanations.