Subject: Re: bug#69939: test utility bug: "test -a -a -a" and "test -o -o -o" fail

From: Pádraig Brady <[email-masked]>
Date: Fri, 22 Mar 2024 16:45:19 +0000

On 22/03/2024 11:20, Vincent Lefevre wrote:
> With GNU Coreutils 9.4, both "test -a -a -a" and "test -o -o -o" fail:
> 
> $ export POSIXLY_CORRECT=1
> $ /usr/bin/test -a -a -a ; echo $?
> /usr/bin/test: ‘-a’: unary operator expected
> 2
> $ /usr/bin/test -o -o -o ; echo $?
> /usr/bin/test: ‘-o’: unary operator expected
> 2
> 
> According to POSIX, they should return 0.
> 
> https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html
> says for 3 arguments:
> 
>    If $2 is a binary primary, perform the binary test of $1 and $3.
> 
> Here, $2 is -a and -o respectively, which are binary primaries.
> And both $1 and $3 are non-null strings.

Agreed. Any leading '-' triggers this:

   $ env test -. -a -.
   test: ‘-.’: unary operator expected

Note we already advise (as does POSIX) to avoid '-a' and '-o':
https://debbugs.gnu.org/22909

BTW POSIX advises to use parenthesis to avoid some parsing ambiguities,
and it helps in this case too:

   $ env test \( -a \) -a \( -a \); echo $?
   0

Though I see bash 5.2.26 has issue with it :/

   $ test \( -a \) -a \( -a \)
   bash: test: `)' expected

bash doesn't like the -a in particular, and is ok with other strings:

   $ test \( -. \) -a \( -. \); echo $?
   0

cheers,
Pádraig