Lisandro Maders
Lisandro Maders

Reputation: 13

Passing a variable twice in Bash

I want the command: echo "$string4" to return me the values of the SETFIELDS_X_XX variables defined in the beginning of my file, during the loop. However, it only returns me the name SETFIELDS_X_XX. What am I doing wrong?

SETFIELDS_2_25=225
SETFIELDS_2_200=2200
SETFIELDS_2_500=2500
SETFIELDS_10_25=1025
SETFIELDS_10_200=10200
SETFIELDS_10_500=10500
SETFIELDS_10_1000=101000
SETFIELDS_50_25=5025
SETFIELDS_50_200=50200
SETFIELDS_50_500=50500
SETFIELDS_50_1000=501000

for LBUB in 2 10 50
do
    for generations in 25 200 500 1000
    do
        string1="SETFIELDS_"
        string2="$LBUB"
        string3="_$generations"
        string4=$string1$string2$string3
    #echo "LBUB = $LBUB"
    #echo "generations = $generations"
    echo "$string4"
    done
done

Upvotes: 1

Views: 1095

Answers (1)

Lohit Gupta
Lohit Gupta

Reputation: 1081

Use this instead:

echo ${!string4}

It will print the value stored inside the variable SETFIELDS_X_XX

Upvotes: 1

Related Questions