如何在bash中导出关联数组(哈希)? [英] How to export an associative array (hash) in bash?
问题描述
相关但并非重复的内容:如何在Bash中定义哈希表?
Related, but not a duplicate of: How to define hash tables in 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屋!