Use bash
indirect variable reference:
${!var}
And of course can be done with eval
, not recommended:
eval 'echo $'"$var"
Why:
$ bar=xyz
$ var='bar;whoami'
$ eval 'echo $'"$var"
xyz
spamegg
Th command whoami
is being evaluated too as part of evaluation by eval
, imagine a destructive command instead of whoami
.
Example:
$ bar='xyz'
$ var=bar
$ echo "${!var}"
xyz
$ eval 'echo $'"$var"
xyz
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…