Using variable value as variable name in bash

試著忘記壹切 提交于 2021-02-08 11:59:36

问题


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:


回答1:


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

Output:

59

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


来源:https://stackoverflow.com/questions/33981637/using-variable-value-as-variable-name-in-bash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!