Reputation: 7333
In bash, if I have:
y=10
x='y'
echo $x # prints 'y'
Now I want to get $y
via $x:
echo ${$x} # error: "bad substitution"; I want to print 10
How do I lookup the variable value with name $x
?
Upvotes: 3
Views: 1449
Reputation: 16440
Use eval
for indirect references while escaping the outer dollar sign
eval echo "\${$x}"
To assign to a variable
eval "z=\${$x}"
echo "$z"
# 10
Upvotes: 0