Le Mon, Dec 22, 2025 at 12:25:26PM -0500, Greg Wooledge a écrit :
> 
> > I'm not sure how to correctly use @K for this!
> 
> Your solution begins with a list of alternating keys and values.  @K helps
> when you're starting with an associative array instead of a list, and
> you use it with eval:
I was spoken about *indexed array*, not associatives.

>     declare -A aa=( empty '' inject '$(date)' "space key" "space value" \
>         lb \[ rb \] punc $' \t\n' )
>     kvstring="${aa[*]@K}"
>     eval "declare -A newarray=($kvstring)"
Yes, but

  $ declare -A aa=( empty '' inject '$(date)' "space key" "space value" \
        lb \[ rb \] punc $' \t\n' )
  $ printf -v tmpString '[%q]=%q ' "${aa[@]@k}"
  $ declare -A "newarray=($tmpString)"
  $ declare -p newarray 
  declare -A newarray=([inject]="\$(date)" [lb]="[" ["space key"]="space value" 
[e
  mpty]="" [punc]=$' \t\n' [rb]="]" )

seem work too! (Notice use of double-quoted lower @k).

Note: there's no ``eval'', but `declare -A` work like an eval!
If instead of ``%q'', I've used ``%s'', then ``$(date)'' will be executed!

  $ declare -A aa=( empty '' inject '$(date)' "space key" "space value" \
        lb \[ rb \] punc $' \t\n' )
  $ printf -v tmpString '[%q]="%s" ' "${aa[@]@k}"
  $ declare -A "newarray=($tmpString)"
  $ declare -p newarray
  declare -A newarray=([inject]="Mon Dec 22 19:57:41 CET 2025" [lb]="[" ["space 
ke
  y"]="space value" [empty]="" [punc]=$' \t\n' [rb]="]" )

-- 
 Félix Hauri  -  <[email protected]>  -  http://www.f-hauri.ch

  • Question about... 王伟
    • Re: Quest... Greg Wooledge
      • Re: Q... Koichi Murase
        • R... Greg Wooledge
          • ... Zachary Santer
            • ... Greg Wooledge
          • ... Koichi Murase
            • ... Koichi Murase
          • ... Félix Hauri via Bug reports for the GNU Bourne Again SHell
            • ... Greg Wooledge
              • ... Félix Hauri via Bug reports for the GNU Bourne Again SHell
                • ... Félix Hauri via Bug reports for the GNU Bourne Again SHell
                • ... Greg Wooledge

Reply via email to