[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:39:59 +0100 |
On Tue, 29 Aug 2023 11:34:21 -0400
Chet Ramey <chet.ramey@case.edu> wrote:
> On 8/29/23 11:30 AM, Kerin Millar wrote:
> > 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
>
> That isn't set; you have to assign a value to set a variable. For instance,
Fair enough.
> `export foo' does not result in `foo' being set. What he's really
> interested in is whether the array has any set elements.
Yes, hopefully.
--
Kerin Millar
Re: test -v difference between bash 5.1 and 5.2, Kerin Millar, 2023/08/29