Thursday, December 30, 2021

[SOLVED] how to call a string as a variable in shell script?

Issue

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


Solution

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.



Answered By - daidoji70