2

I hope the question makes sense, I would like to do something like:

a=test
b=a
echo ${$b} # should echo test

Basically I'd like $b to expand to the value a and have bash echo out the value of variable $a (since ${a} is test).

What syntax should I use?

1 Answer 1

3
a=test
b=a
echo ${!b} # does echo test
Sign up to request clarification or add additional context in comments.

4 Comments

great, good to know dereferences work with vars ;) I only knew that syntax in bash arrays.
why don't you do it like this?: a=test; b=$a; echo $b
@RaphaelRoth - because what you suggest is not indirection
A more portable alternative (but also quite dangerous if not used carefully) is eval echo \$$b.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.