Search code examples
bashvariablesvariable-names

Using variable value as variable name in bash


I've seen this question asked before but I can't seem to get the grasp of it.

I have this variable that I get from user input read column_number

and then I join it with the "col" prefix to form the name of my variable

selected_column="col"$column_number

But when I try to evaluate it to get the result, I keep getting the (standard_in) 1: syntax error

sum=$(round $sum+"echo ${!selected_column}", 2)

full code:


Solution

  • column_number=5
    selected_column=col$column_number
    col5=42
    sum=17
    echo $(($sum+${!selected_column}))
    

    Output:

    59
    

    sum=$(round $(($sum+${!selected_column})) 2)