-1

I have config file where variables are defined

A=b c d
b=BALL
c=CAT
d=DOG

This is my script:

for word in $A
do
    for config in $word
    do
        echo $config
    done
    echo "End of nested for loop"
done

Above prints

b
c
d

I need values of b c and d

Ball
CAT 
DOG
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
Elvish_Blade
  • 1,220
  • 3
  • 12
  • 13

3 Answers3

2

You have to access the variable that is pointed by your variable, not your variable itself:

for word in $A
do
    for config in ${!word}
    do
        echo $config
    done
    echo "End of nested for loop"
done

Here you can find more information

Poshi
  • 5,332
  • 3
  • 15
  • 32
1

You can use the following :

#!/bin/bash

A="b c d"
b=BALL
c=CAT
d=DOG


for word in $A
do
    for config in $word
    do
        echo ${!config}
    done
    echo "End of nested for loop"
done
Gautam
  • 1,862
  • 9
  • 16
1

You logically only have a single loop, not a nested one, so you also should implement only a single loop:

for variable in $A
do
  echo "${!variable}"
done

Btw, you will need to have quotes around the assignment with spaces if you simply execute it in the bash:

A='a b c'
Alfe
  • 56,346
  • 20
  • 107
  • 159