在bash中使用变量值作为变量名 [英] Using variable value as variable name in bash

查看:69
本文介绍了在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)

这篇关于在bash中使用变量值作为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆