kino lucky
kino lucky

Reputation: 1405

How to get a variable by another one in bash?

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

Upvotes: 0

Views: 40

Answers (2)

chepner
chepner

Reputation: 531125

An eval-less answer:

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

Upvotes: 2

perreal
perreal

Reputation: 97948

Using eval:

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

Upvotes: 1

Related Questions