-
Notifications
You must be signed in to change notification settings - Fork 1.9k
SC2082
koalaman edited this page May 13, 2015
·
6 revisions
var_1="hello world"
n=1
echo "${var_$n}"
var_1="hello world"
n=1
name="var_$n"
echo "${!name}"
You can expand a variable var_1 with ${var_1}, but you can not generate the string var_1 with an embedded expansion, like ${var_$n}. Instead, you have to use an indirect reference.
You do this by creating a variable containing the variable name you want, e.g. myvar="var_$n" and then expanding it indirectly with ${!myvar}. This will give the contents of the variable var_1.
None