Bug 845925 - test -n always returns 0 if evaluating variable
Summary: test -n always returns 0 if evaluating variable
Keywords:
Status: CLOSED NOTABUG
Alias: None
Product: Fedora
Classification: Fedora
Component: coreutils
Version: 17
Hardware: Unspecified
OS: Unspecified
unspecified
unspecified
Target Milestone: ---
Assignee: Ondrej Vasik
QA Contact: Fedora Extras Quality Assurance
URL:
Whiteboard:
Depends On:
Blocks: 845927 845929
TreeView+ depends on / blocked
 
Reported: 2012-08-06 07:12 UTC by Martin Kyral
Modified: 2012-08-06 07:27 UTC (History)
5 users (show)

Fixed In Version:
Clone Of:
: 845927 845929 (view as bug list)
Environment:
Last Closed: 2012-08-06 07:27:52 UTC
Type: Bug
Embargoed:


Attachments (Terms of Use)

Description Martin Kyral 2012-08-06 07:12:52 UTC
Description of problem:
According to the man page, "test -n STRING" returns true (0) if the STRING's length is non-zero and that it equivalent to "test STRING" (and contrary to "test -z STRING").

Version-Release number of selected component (if applicable):
coreutils-8.15-7.fc17.x86_64

How reproducible:
Always


Steps to Reproduce:
1. VAR=''
2. test -n $VAR ; echo $?
3. test $VAR ; echo $?

  
Actual results:
In step 2, the output is 0 (true in shell semantics).


Expected results:
In step 2, the output is 0 (false in shell semantics).

Additional info:
"test -n $VAR" returns 0 on both empty and non-empty string $VAR value. "test $VAR" and "test -z $VAR" give the expected values, ie. 0,1 on non-empty and 1,0 on empty $VAR values. Also, "test -n" gives correct output if a direct value is given.

Comment 1 Martin Kyral 2012-08-06 07:22:11 UTC
Expected results:
In step 2, the output is 1 (false in shell semantics).

Sorry for the typo..

Comment 2 Kamil Dudka 2012-08-06 07:27:52 UTC
(In reply to comment #0)
> 2. test -n $VAR ; echo $?

You need to write:

test -n "$VAR"

... so that "test" actually gets an empty string.  Otherwise shell will throw the empty string away.

Additionally, you are probably using the shell built-in command "test", not the executable /usr/bin/test from coreutils:

http://www.gnu.org/software/coreutils/faq/coreutils-faq.html#I-am-having-a-problem-with-kill-nice-pwd-sleep-or-test_002e


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