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    c...@case.edu    http://tiswww.cwru.edu/~chet/


Reply via email to