bug-bash
[Top][All Lists]
Advanced

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

Re: Strange (wrong?) behaviour of "test ! -a file"


From: Chet Ramey
Subject: Re: Strange (wrong?) behaviour of "test ! -a file"
Date: Sun, 21 Oct 2018 13:03:06 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:52.0) Gecko/20100101 Thunderbird/52.9.1

On 10/21/18 5:32 AM, Martin Schulte wrote:
> Hello,
> 
> "help test" states that "-a file" and "-e file" do them same ("True if
> file exists.")
> 
> This is not true when negating the result as you can see from the output
> below. The bash builtin even behaves different than the test from
> coreutils.

This is documented behavior consistent with the POSIX standard. It's been
this way for so long, there is even a question in the long-dormant FAQ
about it (E1).

The `test' command's behavior is determined by the number of arguments,
as specified by POSIX:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128

The help text for test says

"The behavior of test depends on the number of arguments.  Read the
bash manual page for the complete specification."

The man page says:

      3 arguments
             The following conditions are applied in the order listed.
             If  the  second argument is one of the binary conditional
             operators listed above under CONDITIONAL EXPRESSIONS, the
             result of the expression is the result of the binary test
             using the first and third arguments as operands.  The  -a
             and  -o  operators  are  considered binary operators when
             there are three arguments.

This is the order in which the POSIX algorithm specifies the conditions.

> It looks as if "! -a file" results in "( ! ) -a ( file )" in bash while
> it results in "! ( -a file )" in coreutils' test.

Coreutils does the tests in a different order than they appear in the POSIX
standard.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/



reply via email to

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