bug-bash
[Top][All Lists]
Advanced

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

Re: Antw: Re: "set -u" and empty arrays


From: Eli Schwartz
Subject: Re: Antw: Re: "set -u" and empty arrays
Date: Tue, 14 May 2019 09:03:13 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1

On 5/14/19 2:53 AM, Ulrich Windl wrote:
>>>> Chet Ramey <chet.ramey@case.edu> schrieb am 13.05.2019 um 21:44 in 
>>>> Nachricht
> <ead04541-953d-f1ba-f021-5c6938063f53@case.edu>:
>> On 5/9/19 7:07 AM, Ulrich Windl wrote:
>>> Bash Version: 4.3
>>> Patch Level: 48
>>> Release Status: release
>>>
>>> Hi!
>>>
>>> Is this intended with "set -u":
>>> # set -u
>>> # declare -a INSTANCES
>>> # echo ${INSTANCES[*]}
>>> bash: INSTANCES[*]: unbound variable
>>> # INSTANCES=()
>>> # echo ${INSTANCES[*]}
>>> bash: INSTANCES[*]: unbound variable
>>> # echo ${INSTANCES[@]}
>>> bash: INSTANCES[@]: unbound variable
>>
>> This was changed late in the bash-4.4 development cycle (July, 2016). From
>> bash-4.4/CHANGES:
>>
>> a.  Using ${a[@]} or ${a[*]} with an array without any assigned elements 
>> when
>>     the nounset option is enabled no longer throws an unbound variable 
>> error.
> 
> Thanks for the info! Actually it seems "${#a[*]}" is also affected:
> v04:~> declare -a a
> v04:~> echo ${#a[@]}
> 0
> v04:~> set -u
> v04:~> echo ${#a[@]}
> bash: a: unbound variable
> v04:~> a=()
> v04:~> echo ${#a[@]}
> 0
> v04:~> echo $BASH_VERSION
> 4.3.48(1)-release

That's not an array, though, so its behavior there is expected -- and in
fact, I get the same results in bash 5.0.7(1)-release

When "a" is an unbound array declared with declare -a, its non-array
"how long am I" variable doesn't have an integer inside it, not even
"0". When "a" is a bound array declared with =(), it has a defined
length of 0. So the non-array variable "${#a[@]}"=0, and bash is happy
and continues on with life and doesn't fatally terminate in the middle
of your script in an unexpected fashion.

-- 
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User

Attachment: signature.asc
Description: OpenPGP digital signature


reply via email to

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