Home > Blockchain >  how to call a string as a variable in shell script? [duplicate]
how to call a string as a variable in shell script? [duplicate]

Time:09-21

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

CodePudding user response:

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.

CodePudding user response:

Just use the following in bash:

echo ${!hi}

or eval in other shells

eval "echo \$$hi"
  • Related