如何在 bash 中导出关联数组(哈希)? [英] How to export an associative array (hash) in bash?

查看:18
本文介绍了如何在 bash 中导出关联数组(哈希)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相关,但不重复:如何在 Bash 中定义哈希表?

我可以定义和使用 bash 哈希,但我无法导出它,即使使用 -x 标志也是如此.例如,以下用于导出(并测试导出)普通字符串变量:

I can define and use a bash hash, but I am unable to export it, even with the -x flag. For example, the following works to export (and test exportation of) a normal string variable:

aschirma@graphics9-lnx:/$ export animal_cow="moo"
aschirma@graphics9-lnx:/$ bash -c "echo $animal_cow"
moo
aschirma@graphics9-lnx:/$ 

但是,如果我尝试导出哈希:

However, if I try to export a hash:

aschirma@graphics9-lnx:/$ declare -A -x animals
aschirma@graphics9-lnx:/$ animals[duck]="quack"
aschirma@graphics9-lnx:/$ echo ${animals[duck]}
quack
aschirma@graphics9-lnx:/$ bash -c "echo ${animals[duck]}"

aschirma@graphics9-lnx:/$ 

似乎嵌套的 bash shell 在其范围内没有散列.我也通过手动输入嵌套的 bash shell 并尝试以交互方式使用哈希来验证这一点.

It seems the nested bash shell does not have the hash in its scope. I did verify this also by manually entering the nested bash shell and attempting to use the hash interactively.

推荐答案

将数组变量编码到环境中并没有真正的好方法.参见http://www.mail-archive.com/bug-bash@gnu.org/msg01774.html(Chet Ramey 是 bash 的维护者)

There isn't really a good way to encode an array variable into the environment. See http://www.mail-archive.com/bug-bash@gnu.org/msg01774.html (Chet Ramey is the maintainer of bash)

这篇关于如何在 bash 中导出关联数组(哈希)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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