help-bash
[Top][All Lists]
Advanced

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

Re: How to assign one associative array to another?


From: Peng Yu
Subject: Re: How to assign one associative array to another?
Date: Tue, 6 Apr 2021 10:00:53 -0500

On Tue, Apr 6, 2021 at 1:39 AM Koichi Murase <myoga.murase@gmail.com> wrote:
>
> 2021年3月31日(水) 2:47 Peng Yu <pengyu.ut@gmail.com>:
> > Is there a better to do an associative array
> > assignment? Thanks.
> >
> > declare -A x=([a]=1 [b]=2)
> > source <(declare -p x | sed 's/\<x\>/y/')
>
> Not so much different from yours, but one could use ${x[@]@A} to get

What is the difference between `echo "${x[@]@A}"` and `declare -p x`.
Are they exact the same?

$ declare -A x=( [a b]=1 [c]=2 )
$ echo "${x[@]@A}"
declare -A x=([c]="2" ["a b"]="1" )
$ declare -p x
declare -A x=([c]="2" ["a b"]="1" )

> the definition. Also, one can use ${var/ *=/ y=} to replace the
> variable name or use ${var#*=} to strip off unnecessary parts. In this
> way, we don't have to fork / exec other processes.
>
> This
>
>   declare -A x=([a]=1 [b]=2)
>   def=${x[@]@A}
>   eval "${def/ x=/ y=}"
>   declare -p y
>
> Or this
>
>   declare -A x=([a]=1 [b]=2)
>   def=${x[@]@A}
>   eval "declare -A z=${def#*=}"
>   declare -p z
>
> --
> Koichi



-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]