Indirectly reference bash variable

If you know the name of a variable which contains a value of interest, you need indirect referencing to retrieve the value, this is how you do it.


j=1 #variable containg a value
i=j #variable containing the name of the variable which holds the value
eval k=\$$i #k now contains the value of j which is 1
echo $k
k=${!i}  #another way of indirectly referencing
echo $k

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.