bug-bash
[Top][All Lists]
Advanced

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

Re: test -v difference between bash 5.1 and 5.2


From: Chet Ramey
Subject: Re: test -v difference between bash 5.1 and 5.2
Date: Tue, 29 Aug 2023 11:24:43 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.14.0

On 8/29/23 10:32 AM, Christian Schneider wrote:
Hi all,

not sure if this intended or not, but in bash 5.2-p15 one of our scripts is broken. it is related to test -v, that checks, if a variable is set together with arrays.

Yes. Bash-5.2 allows test -v assoc[@] to test whether an associative array
has key `@' set. There was no way to do this in bash-5.1.

If you want to check whether an array variable is set, you can check
whether it has any set elements:

(( ${#assoc[@]} > 0 ))

which will work on bash-5.1 and bash-5.2.

If you set the shell compatibility level to 51, you can get the bash-5.1
behavior on bash-5.2. This is the relevant portion of description of
`compat51' from the bash info manual:

        * 'test -v', when given an argument of 'A[@]', where A is an
          existing associative array, will return true if the array has
          any set elements.  Bash-5.2 will look for and report on a key
          named '@'

There was a long discussion about this as part of a bigger 2021 thread
about how bash expanded associative array subscripts, starting at:

https://lists.gnu.org/archive/html/bug-bash/2021-04/msg00058.html

--
``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]