相关但不重复:How to define hash tables in Bash?
我可以定义和使用 bash 散列,但我无法导出它,即使使用 -x 标志也是如此。例如,以下用于导出(和测试导出)普通字符串变量:
aschirma@graphics9-lnx:/$ export animal_cow="moo"
aschirma@graphics9-lnx:/$ bash -c "echo \$animal_cow"
moo
aschirma@graphics9-lnx:/$
但是,如果我尝试导出哈希:
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 并尝试以交互方式使用哈希来验证这一点。
最佳答案
没有真正好的方法将数组变量编码到环境中。请参阅 http://www.mail-archive.com/bug-bash@gnu.org/msg01774.html (Chet Ramey 是 bash 的维护者)
关于bash - 如何在 bash 中导出关联数组(哈希)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12944674/