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: Koichi Murase
Subject: Re: How to assign one associative array to another?
Date: Tue, 6 Apr 2021 15:39:01 +0900

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
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



reply via email to

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