Harald Dunkel wrote:
> Package: coreutils
> Version: 5.2.1-2

Coreutils?  Or Bash?

  type test
  test is a shell builtin

It does not matter in this case because this is not a bug.  But if it
were a bug then it would matter.

> IMHO
>       x=""; test -n $x && echo true
> 
> should either produce the same result as
> 
>       test -n "" && echo true
> 
> , or the first version should produce an error message
> due to the missing argument. But it doesn't.

See the POSIX docs here:

  http://www.opengroup.org/onlinepubs/009695399/utilities/test.html

Look for this section:

  1 argument:
      Exit true (0) if $1 is not null; otherwise, exit false.

The problem this is solving is this one:

  var=
  test "$var" && echo true || echo false

  var=foo
  test "$var" && echo true || echo false

  var=-n
  test "$var" && echo true || echo false

The first should be false but the last two should be true.  The value
of -n as an arbitrary string in test "-n" cannot be confused with the
"test -n STRING" operator due to the wording of the specification.
This is standardized to depend upon the numbar of arguments to the
routine.  If there is only one argument then it is not an operator and
instead is only dependent upon it being non-null.

Your two cases are different.

>       x=""; test -n $x && echo true

The shell will expand $x and finding nothing there will pass no
argument for it to the command.  The test command is simply "test -n"
and nothing more.  Because the first arg is not a null string it will
return true.

This is an incorrect syntax to use for the shell.  it also does not
guard against further word splitting.

  x="one     two" ; echo $x
  one two
  x="one     two" ; echo "$x"
  one     two

In the raw case without quoting $x the spaces in the variable are lost
due to the shell splitting the word around the IFS.  Proper quoting is
required for robust operation.

>       test -n "" && echo true

I guess derived from:

  x=""; test -n "$x" && echo true

This case is different because there is always going to be another
argument and -n will be an operator looking to see if the argument is
non-zero.  This is good shell usage.

Bob


-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to