是否可以从 bash 中的其他变量构建变量名? [英] Is it possible to build variable names from other variables in bash?

查看:27
本文介绍了是否可以从 bash 中的其他变量构建变量名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为这个非常糟糕的标题 - 以及低质量的帖子 - 但我基本上想做的是:

I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:

for I in 1 2 3 4
    echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.

显然上述方法不起作用 - 它(我认为)会尝试回显名为 $VAR$I 的变量在 Bash 中是否可行?

Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I Is this possible in Bash?

推荐答案

是的,但不要那样做.改用数组.

Yes, but don't do that. Use an array instead.

如果你仍然坚持这样做......

If you still insist on doing it that way...

$ foo1=123
$ bar=foo1
$ echo "${!bar}"
123

这篇关于是否可以从 bash 中的其他变量构建变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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