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: Kerin Millar
Subject: Re: test -v difference between bash 5.1 and 5.2
Date: Tue, 29 Aug 2023 16:30:02 +0100

Hi,

On Tue, 29 Aug 2023 16:32:36 +0200
Christian Schneider <cschneider@radiodata.biz> 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.
> 
> I condensed it to following example:
> #!/bin/bash
> 
> declare -A foo
> foo=(["a"]="b" ["c"]="d")
> declare -a bar
> bar=("a" "b" "c")
> declare -a baz
> baz=("foo" "bar")
> for i in "${baz[@]}" ; do
>          echo $i
>          if [ ! -v "$i"[@] ] ; then
>                  echo "$i not set"
>          fi
> done
> --------
> 
> with bash 5.2-p15 the output of this script is
> foo
> foo not set
> bar

It pertains to the following change.

j. Associative array assignment and certain instances of referencing (e.g.,
   `test -v' now allow `@' and `*' to be used as keys.

For now, you have the option of setting the compatibility level to 51.

Incidentally, your code is defective to begin with. That is, it doesn't 
actually prove that an array variable is set, even with 5.1.

$ declare -p BASH_VERSION
declare -- BASH_VERSION="5.1.16(1)-release
$ declare -A map; [[ -v 'map[@]' ]]; echo $?
1

Frankly, the only interface that I would trust for this is declare -p, which is 
a wasteful one; there is no way to instruct the declare builtin to refrain from 
writing out the elements of an array.

-- 
Kerin Millar



reply via email to

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