How to set a variable name from another variables value?

Experts,

I want to set value of variables like this in bash shell:

i=5 ; L=100

I want variable d5 (that is d(i) ) to be assign the value of $L ,

d$i=$L ;  echo $d5

Not working

Thanks.,

You can use eval in this scenario:

eval d$i=$L ;  echo $d5

But I recommend to stay away from eval because using it is a security risk.

Array is the best choice for you here:

d[$i]=$L ;  echo ${d[$i]}
1 Like

As long as your script controls setting $i and $L (not letting a malicious user provide input that could cause $i or $L to expand to something like

$(do something)

that could destroy any data accessible by your script), the following should do what you want:

i=5;L=100
eval d$i=$L
echo $d5
1 Like

Yoda, Thanks a lot it worked perfectly Don thanks a lot , it worked too and thanks for explaining that the variable could expand with wrong entries & could cause $i or $L to expand .