bug-bash
[Top][All Lists]
Advanced

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

Re: This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo ${#


From: Chet Ramey
Subject: Re: This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo ${#foo[@]}
Date: Thu, 7 Nov 2024 15:10:08 -0500
User-agent: Mozilla Thunderbird

On 11/6/24 9:38 PM, David Linden wrote:

Bash Version: 4.4
Patch Level: 20
Release Status: release

Description:
        This errors in 4.4, did not in 4.2: set -u; declare -A foo; echo 
${#foo[@]}

Yes, this was a bug in bash-4.2, fixed in bash-4.3. A variable isn't set
until it's been assigned a value. This fix aligns the array variable
behavior with the scalar (non-array) variable behavior.


        How am I supposed to determine that a declared associative array is 
empty?

That's not the question that `set -u' answers. It will tell you whether
a variable with attributes (or without) has been assigned a value.

Does your code manage this variable? If it does, you should be able to
determine whether or not it was ever assigned a value, or make sure
that it has been assigned a value, if that's important. The empty array
is a valid value, just like the empty string is a valid value for scalar
variables.

        Or even use it in a conditional even one where the value won't be 
evaluated?

What do you mean? Using something like foo[@] is fine in expansions
where it won't be expanded:

echo ${foo[@]-unset}

or

v=set; echo ${v:-foo[@]}

But if you get into a case where the variable needs to be expanded,
you're going to get an error if the variable isn't set.

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

Attachment: OpenPGP_signature.asc
Description: OpenPGP digital signature


reply via email to

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