[Top][All Lists]

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

Re: ${y@a} on an empty array y fails with 'y: unbound variable' if run w

From: Chet Ramey
Subject: Re: ${y@a} on an empty array y fails with 'y: unbound variable' if run with set -u
Date: Tue, 26 Oct 2021 10:02:48 -0400
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:91.0) Gecko/20100101 Thunderbird/91.2.0

On 10/25/21 8:24 PM, Mark March wrote:
> If -u is on and you declare a simple or associative array 'y' and set it 
> empty, then ${y@a} will fail with 'unbound variable'.

It really is unset:

"An array variable is considered set if a subscript has been assigned  a

> I was able to repro this under 5.1.0(1)-release and 5.1.8. 5.0.17(1)-release 
> does not seem to be affected.

Bash-5.1 fixed some bugs in this area.

> The code to reproduce (output lines begin with #>):
> #> 5.1.0(1)-release
> set -u
> declare -a y=()
> echo ${y@a}
> #> bash: y: unbound variable

Yes, because it's unset, but see below.

> declare -p y
> #> declare -a y=()
> echo ${y[@]}
> #>

The `@' and `*' subscripts are exempted from the nounset option to parallel
the behavior of `$@' and `$*' when there are no positional parameters (the
latter is specified by POSIX).

> set +u
> echo ${y@a}
> #> a
> As you can see, turning off -u makes ${y@a} work correctly. 

If the `nonunset' option is not enabled, there is a special case for the
`a' transform so it will print the attributes of an unset array variable.

That was the result of a long discussion:


> I wonder if this is a side-effect of the fix that you described in item (n) 
> of the most recent change log:
> n. Fixed a bug that caused ${foo@a} to treat foo as an unset variable if it 
> was an array without a value for subscript 0/"0" but had other set elements

This is not relevant, there are no set elements.

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