0

Heres the bash script:


ALL="2 3"

VC2="https://someaddress.acme.com"
VC3="https://someotheraddress.acme.com"

hosts2='12
13
14'

hosts3='15
16
17'

for i in $ALL
do

Here is where i want the substitution but no matter what i use it doesn't work. I tried all of the following

     VC="$VC$i"
     VC="${VC$i}"
     VC="$(VC$i)"
     VC=$(VC$i)

     HOSTS="$hosts$i"
     echo "$VC"
     echo "$HOSTS"

the output i need is for each of the numbers in the ALL variable to then equal the predefined VC and hosts variables. i.e.

When i=2:

i=2
VC="https://someaddress.acme.com"
HOSTS='12
13
14'

and when i=3

i=3
VC="https://someotheraddress.acme.com"
HOSTS='15
16
17'

Thanks in advance.

Thomas

tom
  • 1
  • 1
  • You can use indirection / namerefs as described here [Bash: Echo a variable whose name is the value of another variable](https://unix.stackexchange.com/questions/270140/bash-echo-a-variable-whose-name-is-the-value-of-another-variable) however consider using indexed array variables instead. – steeldriver Jun 30 '21 at 22:16
  • 1
    Better, use arrays instead of splitting strings on either space or newline – roaima Jun 30 '21 at 23:14
  • I did see the array route, i couldn't figure out how it was pertinent, i wasn't splitting anything. i am replacing variables!.. thanks. – tom Jul 01 '21 at 02:46
  • @steedrivers answer did the trick.. i saw references to arrays, but i couldn't get it to work for my example. – tom Jul 01 '21 at 02:58

0 Answers0