How to assign one value of a variable to another variable in a shell script

I tried using the shell code to get the values templike "good 1" and "good 2", but I could not.

hello_1="good 1"
hello_2="good 2"
for i in 1 2
do
temp="hello_$i"
echo $temp
done

I want to get the value hello_1and hello_2variables in a variable temp. I tried the above script, but I was not able to get the values hello_1and hello_2in temp. please help me.

+4
source share
1 answer

If you are using bash 4or above, read the indirect links . But to answer your question, you can do

hello_1="good 1"
hello_2="good 2"
for i in 1 2
do
    eval temp="\$hello_$i"
    echo $temp
done

Output

good 1
good 2

eval evil . .

, eval .

 eval temp="$hello_1"

, $i 1, \\ char \$hello_1,   eval $hello_1 temp, ..

 temp="good 1"

IHTH

+4

Source: https://habr.com/ru/post/1569256/


All Articles