fthinker
fthinker

Reputation: 667

Bash indirect reference where one var references multiple

I am trying to dereference two or more variables contained in a single variable but I can't think of how to do it without eval:

$:b=5
$:c=10
$:a='$b $c'
$:result=`eval echo $a`
$:echo $result
5 10

I'm looking to do the equivalent operation that gives me 'result' so I don't have to do an eval. Is it possible to resolve multiple variables referenced by a single variable any other way? I'm looking to do this in BASH

Upvotes: 2

Views: 1220

Answers (2)

kwarrick
kwarrick

Reputation: 6230

In short, no I think that you have to use an eval for multi-variable indirect references as you have in your code.

There appear to be two types of indirect variable reference in Bash, one with eval and one with the special notation ${!var}:

$ b=5
$ a=b
$ echo ${!a}
5

Perhaps using an array would meet your needs:

$ b=5
$ c=10
$ a=(b c)
$ echo ${!a[0]} ${!a[1]}
5 10

http://tldp.org/LDP/abs/html/abs-guide.html#IVR

Upvotes: 4

Karoly Horvath
Karoly Horvath

Reputation: 96286

eval sound quite scary...

here is a better solution:

Bash Templating: How to build configuration files from templates with Bash?

you just have to tweak it a bit so it follows your syntax.

Upvotes: 1

Related Questions