Re: Question about associative array runtime assignment vs literal initialization

Greg Wooledge <[email protected]>
Newsgroups gmane.comp.shells.bash.bugs
Message-ID <[email protected]>
On Mon, Dec 22, 2025 at 23:01:39 +0800, Koichi Murase wrote:
> It's similar, but it seems slightly different. The present report
> seems to discuss a different point. « declare -A aa="( "${kv[@]}" )"»
> and « declare -A aa=( "${kv[@]}" ) » are different.

The point is NEITHER one of them does what everyone expects.

hobbit:~$ kv=(one 1 two 2 "two and a half" 2.5 three 3)

hobbit:~$ declare -A aa=("${kv[@]}"); declare -p aa
declare -A aa=(["one 1 two 2 two and a half 2.5 three 3"]="" )

hobbit:~$ declare -A aa="("${kv[@]}")"; declare -p aa
declare -A aa=([3]="" [a]="half" [two]="and" [2.5]="three" [one]="1" )

Both of these are "wrong" from the point of view of everybody but Chet.
It doesn't appear this is going to change.

The only known workaround is to abandon this approach entirely, and use
@K plus eval instead.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.