在 unix 中使用变量作为变量名的一部分 [英] Using variables as part of variable name in unix
本文介绍了在 unix 中使用变量作为变量名的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将变量命名为 a_${v}
.
例如:v 可以是 2013 2014
for example : v can be 2013 2014
我现在向 a_${v}
a_${v}=hI # a_2013 should be Hi
v=2014
so a_${v}=Hello # a_2014
应该是你好
我尝试使用 eval 命令,虽然它在分配值时没有抛出错误,但我无法提取变量名称的值
I tried using eval command though it is not throwing error while assigning a value but I am not able to extract the value of variable name
$ v=2013
$ eval a_${v}=Hi
$ v=2014
$ eval a_${v}=Hello
echo ${a_${v}}
不起作用.. :(
我正在使用 bash 并且我不想更改变量名称,即不想将值分配给另一个值
I am using bash and I don't want to change the variable name i.e dn't want to assign the value to another value
推荐答案
在 bash 中,您可以执行以下操作(注意最后一行的感叹号语法):
In bash you can do the below (note the exclamation mark syntax in the last line):
#!/bin/bash
a_2014='hello 2014'
year=2014
varname=a_${year}
echo ${!varname}
这篇关于在 unix 中使用变量作为变量名的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文