On Mon, Dec 8, 2025 at 4:10 PM Chet Ramey <[email protected]> wrote:
>
> On 12/8/25 2:34 PM, Greg Wooledge wrote:
> >
> > As far as I know, the only safe way to serialize the keys and values of
> > an associative array into a string value, and then reassign that string
> > value back to its constituent associative array later, is to use the @K
> > expansion plus eval:
>
> OK. If you need to do that, then use that method.

This is still going to be the natural thing:

$ declare -a kv=(one 1 'two point five' 2.5 $'\n\t \n' punc)
$ declare -A aa=( "${kv[@]}" )
Could've given you
declare -A aa=([$'\n\t \n']="punc" ["two point five"]="2.5" [one]="1"  )
$ unquoted="one two three four"
$ declare -A bb=( ${unquoted} )
Could've given you
declare -A bb=([three]="four" [one]="two")

since the same works for indexed arrays.

Bash could tell it's not dealing with a subscripted assignment and
expand and word-split whatever appears within the parentheses before
taking those contents as individual keys and values.

Associative array keys aren't word-split, but that makes more visual
sense when they appear between square brackets, as they do in all
other cases than the kvpair compound assignment. The square brackets
serve as double quotes in this case.

(Yes, I know we've been here before.)

Reply via email to