bug-bash
[Top][All Lists]
Advanced

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

Re: Built-in `test -a` returns incorrectly


From: Greg Wooledge
Subject: Re: Built-in `test -a` returns incorrectly
Date: Mon, 18 Nov 2024 10:54:21 -0500

On Mon, Nov 18, 2024 at 11:31:25 +0000, Klüver, Tibor wrote:
>         Built-in `[ -a ... ]` returns true when false, negation with `!` is 
> also very strange.
>         `[[` is not affected, neither is `/bin/[`.
> 
> Repeat-By:
>         * touch file
>         * [ -a file ] && echo exists         => exists
>         * [ ! -a file ] && echo exists       => exists

The -a operator has two separate meanings in the test command.
It can either be "unary" (taking one argument), in which case it's
a synonym for -e.  Or it can be "binary" (taking two arguments),
in which case it's meant to sit in between two other expressions,
and is treated as a Logical AND.

The more complex your test expression becomes, the more likely it is
that the parser will fail to guess your intention, and will use the
other meaning of -a.

Your best bet is simply to *never* use it.  It's a daft synonym for -e,
so if you want to test for file existence, use -e.  If you want a
Logical AND between two test expressions, simply call test twice:

    [ "$x" ] && [ "$y" ]

This avoids all of the problems with -a.



reply via email to

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