bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bug in /bin/test ?


From: Brian Dessent
Subject: Re: bug in /bin/test ?
Date: Sat, 25 Aug 2007 03:19:13 -0700

Emanuele Tomasi wrote:

> $> /usr/bin/test -n $casa && echo yes || echo no
> yes
> 
> $> /usr/bin/test $casa && echo yes || echo no
> no
>
> $> /usr/bin/test -z $casa && echo yes || echo no
> yes

I think you may not be understanding the fact that the shell expands
$casa into the empty string before invoking test, effectively swallowing
it, thus the above are equivalent to:

$ /usr/bin/test -n; echo $?
0

$ /usr/bin/test; echo $?
1

$ /usr/bin/test -z; echo $?
0

That is, the test program does not see any STRING argument at all.  If
you want to test an empty string you have to explicitly quote it in the
shell so that an empty argument is actually passed to the program being
invoked.  Then you get the expected behavior:

$ /usr/bin/test -n "$casa"; echo $?
1

$ /usr/bin/test "$casa"; echo $?
1

$ /usr/bin/test -z "$casa"; echo $?
0

You might have also seen the idiom 'test "x$foo" = x' which is another
way of dealing with this problem, as well as several others such the
case when $foo is "=" or a value that starts with a dash.

Brian




reply via email to

[Prev in Thread] Current Thread [Next in Thread]