在bash中使用变量值作为变量名

在bash中使用变量值作为变量名

问题描述:

我以前看过这个问题,但似乎无法理解.

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

我有一个从用户输入中获取的变量读取column_number

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

然后我用"col"前缀将其加入以形成我的变量的名称

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

selected_column="col"$column_number

但是当我尝试对其进行评估以获得结果时,我不断收到(standard_in)1:语法错误

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)

完整代码:

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

输出:


59


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