可以将关联数组分配给bash中的变量吗? [英] Can you assign associative arrays to variables in bash?

查看:56
本文介绍了可以将关联数组分配给bash中的变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些不同的关联数组作为变量:

I have a few different associative arrays as variables:

declare -A FIRST=( [hello]=world [foo]=bar )
declare -A SECOND=( [bonjour]=monde [fu]=ba )

我想做的是获取第三个变量,并将其分配给另一个变量,例如:

What I'd like to be able to do is take a third variable and assign it to one or the other, something like:

usethisarray=$FIRST

或者也许

declare -a usethisarray=$FIRST

但是这些都不是真正有效的.我可以得到一个间接级别来指向我需要的关联数组吗?

But neither of those really work. Can I get a level of indirection to point at the associative array I need?

推荐答案

bash具有可变间接寻址,但是使用起来有点麻烦:

bash has variable indirection but it's kind of a pain to use:

$ declare -A FIRST=( [hello]=world [foo]=bar )
$ alias=FIRST
$ echo "${!alias[foo]}"

$ item=${alias}[foo]
$ echo ${!item}
bar

这篇关于可以将关联数组分配给bash中的变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆