bash 从bash中的for循环回显变量名称,而不是值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18985452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 06:40:02  来源:igfitidea点击:

Echo variable name, not value, from for loop in bash?

linuxbash

提问by cashman04

a=1
b=2
c=3

for db in $a $b $c; do
echo VARIABLE NAME
blah 
blah  
blah

I need this for a script I am writing. I have some Client names set at the top to database names for the varialble. I am running a ps -ef and a few other things, but I need it to echo out which client name it is on in the loop. So in example above, it would echo out "a", then its other commands, then on the second loop echo "b" .....etc

我需要这个用于我正在编写的脚本。我在顶部为变量的数据库名称设置了一些客户端名称。我正在运行 ps -ef 和其他一些东西,但我需要它来回显循环中它所在的客户端名称。所以在上面的例子中,它会回显“a”,然后是它的其他命令,然后在第二个循环中回显“b”.....等

回答by choroba

Use variable indirection:

使用变量间接:

for var in a b c ; do
    echo $var ${!var}
done