Linux – How to reference the value of a constructed environment variable in a loop

bashlinux

What I'm trying to do is loop over environment variables. I have a number of installs that change and each install has 3 IPs to push files to and run scripts on, and I want to automate this as much as possible (so that I only have to modify a file that I'll source with the environment variables). The following is a simplified version that once I figure out I can solve my problem.

So given in my.props:

COUNT=2
A_0=foo
B_0=bar
A_1=fizz
B_1=buzz

I want to fill in the for loop in the following script

#!/bin/bash
. <path>/my.props

for ((i=0; i < COUNT; i++))
do
  <script here>
done

So that I can get the values from the environment variables. Like the following(but that actually work):

echo $A_$i $B_$i

or

A=A_$i
B=B_$i
echo $A $B

returns foo bar then fizz buzz

Best Answer

$ num=0
$ declare A_$num=42  # create A_0 and set it to 42
$ echo $A_0
42
$ b=A_$num           # create a variable with the contents "A_0"
$ echo ${!b}         # indirection
42

You can iterate num and use declare to create the variables and indirection to reference them. But wouldn't you really prefer to use arrays?

for i in foo bar baz
do
    some_array+=($i)    # concatenate values onto the array
done

or

string="foo bar baz"
some_array=($string)

How about this?

$ cat IPs
192.168.0.1
192.168.0.2
192.168.0.3
$ cat readips
#!/bin/bash
while read -r ip
do
    IP[count++]=ip                 # is it necessary to save them in an array,
    do_something_directly_with ip  # if you use them right away?
done < IPs
Related Topic