[Top][All Lists]

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

Re: [bash 4] 'test -v 1' is never true

From: Alejandro Colomar
Subject: Re: [bash 4] 'test -v 1' is never true
Date: Fri, 25 Nov 2022 22:58:14 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.5.0

Hi Branden!

On 11/25/22 22:40, G. Branden Robinson wrote:
Hi Alex,

At 2022-11-25T22:02:46+0100, Alejandro Colomar wrote:
I wrote a script, and am trying it on many systems.  On RHEL8, which
has bash 4, it didn't work.  I could reduce the problem to the
following command, which never returns true:

     test -v 1;

In Debian Sid, where I develop, and where I have bash 5, that works
fine, and is true if the function or script in which it is has any
arguments.  On zsh(1) it also works fine.

Is this a known bug in bash?
The exact version where I can reproduce it is 4.4.20(1)-release

I don't think it is a bug.  It is unspecified behavior according to

"-v" is not an operator according to POSIX,

Yeah, I know.  I tried dash(1), which at least gives some warning:

    $ cat bug.sh

    echo $1;

    test -v 1 \
    && echo 'arguments';

    $ dash bug.sh

    bug.sh: 5: test: -v: unexpected operator

and I don't recall seeing it
in any shell.  It is therefore interpreted as a string argument.

But `-v` is a specified option of bash(1) (and also, at least, zsh(1)):

    $ help test | grep '\-v'
          -v VAR         True if the shell variable VAR is set.

I find it very useful, since I use `set -Eeufo pipefail` in my scripts, to simplify error handling. When I want to use a variable that might be undefined, I first check if it exists.

   The algorithm for determining the precedence of the operators and the
   return value that shall be generated is based on the number of
   arguments presented to test. (However, when using the "[...]" form,
   the right-bracket final argument shall not be counted in this

   In the following list, $1, $2, $3, and $4 represent the arguments
   presented to test:

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

           If $1 is '!', exit true if $2 is null, false if $2 is not

           If $1 is a unary primary, exit true if the unary test is true,
           false if the unary test is false.

           Otherwise, produce unspecified results.






Attachment: OpenPGP_signature
Description: OpenPGP digital signature

reply via email to

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