[Top][All Lists]

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

Re: ${!variable@operator} does not work for variables without values; i

From: Eli Schwartz
Subject: Re: ${!variable@operator} does not work for variables without values; inconsistencies between present and absent [@] for @A and @a
Date: Wed, 19 Feb 2020 18:49:54 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.2

On 2/19/20 6:18 PM, Arfrever Frehtes Taifersar Arahesis wrote:
> While described problems exist, there were typos in original reproduction 
> steps.
> Corrected output also reveals another bug:
> ${!variable[@]@A} for (indexed or associative) arrays does not include 
> indexes.
Your examples are all (still) broken. Do not try to reproduce bugs using:


Instead, use:

VAR1=(aaa bbb)

Reformulate all test cases where you expect the bbb to be printed, but
it isn't.

$ VAR1=(aaa bbb)
$ ref=VAR1
$ echo "${!ref[@]}"
$ echo "${!ref[@]@A}"
declare -a VAR2='aaa'

$ ref2=VAR1[@]
$ args() { printf '<%s> ' "$@"; printf \\n; }
$ args "${!ref2}"
<aaa> <bbbb>
$ args "${!ref2@A}"

You cannot use ${!ref[@]}, you need the array subscript as part of the
set value of "ref" and then indirectly refer to ref.

$ declare -A VAR2=([foo]=bar [baz]="how interesting")
$ args "${!VAR2[@]}"
<foo> <baz>

From man bash:

       List  of array keys.  If name is an array variable, expands to
the list of array indices (keys) assigned in name.  If name is not an
array, expands to
       0 if name is set and null otherwise.  When @ is used and the
expansion appears within double quotes, each key expands to a separate word.

And, as predicted, if instead of using ${!ref[@]} you use
${!varname[@]}, you get meaningful information. If you try to look at
the array keys of "ref" itself, all it prints is the number 0, because:


and since it is set and non-null...

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]