Bug 340291 - test -n doesnt work
Summary: test -n doesnt work
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: coreutils
Version: 7
Hardware: All
OS: Linux
low
low
Target Milestone: ---
Assignee: Pete Graner
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks:
TreeView+ depends on / blocked
 
Reported: 2007-10-19 18:27 UTC by Stas Sergeev
Modified: 2007-11-30 22:12 UTC (History)
2 users (show)

Fixed In Version:
Doc Type: Bug Fix
Doc Text:
Clone Of:
Environment:
Last Closed: 2007-10-22 08:26:17 UTC
Type: ---
Embargoed:


Attachments (Terms of Use)
the test case (114 bytes, text/plain)
2007-10-19 18:27 UTC, Stas Sergeev
no flags Details

Description Stas Sergeev 2007-10-19 18:27:08 UTC
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 18:27:08 UTC
Created attachment 232901 [details]
the test case

Comment 2 Ignacio Vazquez-Abrams 2007-10-19 18:34:34 UTC
Are you invoking test(1) or bash's built-in test command?

Comment 3 Stas Sergeev 2007-10-19 18:51:26 UTC
test(1) from coreutils.
And using explicitly /usr/bin/test gives
the same, and [ ] too.

Comment 4 Tim Waugh 2007-10-22 08:26:17 UTC
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 18:31:17 UTC
> 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 20:16:50 UTC
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-23 03:53:37 UTC
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 04:01:00 UTC
> 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 04:32:34 UTC
(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 04:54:50 UTC
> No. "$@" is special-cased in bash.
Ah, ok then. And 'test' without any parameters
returns false, right?
Thank you both for the explanations.


Note You need to log in before you can comment on or make changes to this bug.