Bash阵列导出解决方法 [英] Bash array export workaround

查看:69
本文介绍了Bash阵列导出解决方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道以规范的方式导出数组是不可能的,但是我有兴趣找到一种解决方法.我有以下情形:在启动过程中将变量列表从文件加载到数组,并且我需要使该数组对在父环境(. example.sh或仅在父环境中执行或不执行的几个bash脚本可见) example.sh).我尝试了很多事情,但是像这样的事情似乎最有前途:

We know that exporting arrays in a canonical way is impossible, but I am interested in finding a workaround. I have a following scenario: a list of variables is loaded from a file to an array during startup, and I need to have that array visible to several bash scripts that may or may not be executed in the parent environment (. example.sh or just example.sh). I tried many things, but something like this seems as most promising:

export j=1
export array$j=something

然后我尝试使用以下方法访问值:

And then I tried to access the value using:

echo ${array[$j]}      #doesn't work in child script
echo $(echo \$array$j) #displays the actual '$array1' instead of 'something'

有什么建议吗?

推荐答案

您可以使用参数扩展通过间接查找值:

You can look up values through indirection using parameter expansion:

j=1
array1=something

name="array$j"
echo "The value of $name is ${!name}"

这篇关于Bash阵列导出解决方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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