-1

I have a problem that I will not know the name of the variable and the name of the variable will be stored in an array that I have,

the problem here how can I call it, I tried some in the cli to see:

$ hello=sup
$ hi=hello
$ echo $`echo $hi`
$hello

as you see it prints "$hello" instead of "sup" that I want

Amine
  • 11
  • 2

2 Answers2

1

To dereference a variable you have to use the $ so the second assignment isn't working in your code sample.

>hello=sup
>hi=$hello
>echo $hi
sup

See https://tldp.org/LDP/abs/html/ivr.html for more info on indirect variable references.

daidoji70
  • 331
  • 4
  • 13
0

Just use the following in bash:

echo ${!hi}

or eval in other shells

eval "echo \$$hi"
Saboteur
  • 1,331
  • 5
  • 12