简体   繁体   English

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

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

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. 我正在运行ps -ef和其他一些东西,但我需要它来回显它在循环中的客户端名称。 So in example above, it would echo out "a", then its other commands, then on the second loop echo "b" .....etc 所以在上面的例子中,它会回显“a”,然后是其他命令,然后是第二个循环回声“b”.....等

Use variable indirection: 使用变量间接:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM