MechMK1
MechMK1

Reputation: 3378

Call variable based on content of another variable possible in BASH?

Let's say, I have 3 variables, A=1, B=2, C=3 and finally a last variable containing the name of either of them (VAR=A). Is it possible to call A, B, C based on VAR's content without 'if's?

Like echo "${$VAR}"?

Upvotes: 2

Views: 77

Answers (2)

jpalecek
jpalecek

Reputation: 47762

Yes.

eval echo \$$VAR

There is also a bash-only way of doing this, using indirect reference:

echo ${!VAR}

Upvotes: 3

Related Questions