How to get a variable by another one in bash?

筅森魡賤 提交于 2019-12-31 05:34:10

问题


I have three arrays in bash.

 arr1=(arr2 arr3)
 arr2=(1 2 3 4)
 arr3=(6 7 8 9)
 #How can I get a element of arr2 by arr1? like below:
 ${${arr1[0]}[0} # To get first element in arr2

回答1:


Using eval:

eval echo \${${arr1[0]}[0]}



回答2:


An eval-less answer:

tmp=arr1[0]
tmp2=${!tmp}
echo ${!tmp2[0]}


来源:https://stackoverflow.com/questions/15018528/how-to-get-a-variable-by-another-one-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!