Bash – Variable indirection – Using variable contents as a(nother) variable name

This was a tricky action. Assume I have a list of variables, obtained by an external source:




I cannot use loop and in it the phrase ${var$i} (where i is the integer counter). It just doesn’t work. I used this instead to assign the values to an array:

var[$i]=$(eval echo "${var${i}}")

That way, I was able to loop through these values later easily.

So… we can use assigned var names inside a var if we do it right: $(eval echo "${var${i}}")

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

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