Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

linux

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. So in example above, it would echo out "a", then its other commands, then on the second loop echo "b" .....etc

like image 905
cashman04 Avatar asked Dec 02 '22 21:12

cashman04


1 Answers

Use variable indirection:

for var in a b c ; do
    echo $var ${!var}
done
like image 86
choroba Avatar answered Jan 18 '23 22:01

choroba