From:   Lea Gris
Subject: Re: [bug #67782] bash split $() for array initialization but not for 
associative array initialization
Date:   Sun, 7 Dec 2025 13:50:33 +0100

>It is not buggy. You are declaring the array content which is a key
>with an empty value since you are providing 4 arguments to declare:

>- -A
>- a=(
>- $'[A]=a\n[B]=b\n[C]=c'
>- )

>If you need to dynamically declare an associative array, you need to use:

>unset a  ; declare -A a="(
>   $(
>     echo [A]=a
>     echo '[B]=b'
>     echo ['C']=c
>   )
>)"
>echo Nb of elements : ${#a[@]}
>declare -p a

>Nb of elements : 3
>declare -A a=([C]="c" [B]="b" [A]="a" )

Well done.  Always quote your variables!

Note, BTW, that shellcheck catches (i.e., would have caught) this
problem.  Without the quoting, it complains that associative array
assignment requires an index value.  With the quoting, it does not
complain.

=================================================================================
Please do not send me replies to my posts on the list.
I always read the replies via the web archive, so CC'ing to me is unnecessary.

When responding to my posts, please try to refrain from giving bureaucratic 
answers.
If you have nothing useful to say, then just click Next and go on.

Reply via email to