You can use eval for that:
$ var1='< 1 >'; var2='< 2 >'; var3='< 3 >'
$ for i in 1 2 3; do eval echo \$var$i; done
$ for i in 1 2 3; do eval "echo \$var$i"; done
$ for i in 1 2 3; do eval 'echo $var'$i; done
$ for i in 1 2 3; do eval "v=\$var$i"; echo "$v"; done
Take care with the quoting and escaping. If the values contain whitespace or glob characters, the first three will split the values on whitespace, and then expand any filenames that match the glob. This may or may not be what you want. For example:
$ var1='< x >' var2='< * >'
$ for i in 1 2; do eval echo \$var$i; done
< x >
< [list of filenames in current directory] >
(Note that the double spaces around x
were collapsed to single spaces since echo
received <
, x
and >
as separate arguments.)
To work around that, you need to make sure the variable expansion is quoted within the eval
, e.g.:
$ for i in 1 2; do eval echo \"\$var"$i"\"; done
< x >
< * >
(quote the $i
too, as above, unless you know IFS
doesn't contain any digits.)