-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
  • 143,113
  • 25
  • 227
  • 253
Elvish_Blade
  • 1,160
  • 2
  • 11
  • 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
  • 4,817
  • 3
  • 13
  • 28
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,794
  • 6
  • 14
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
  • 52,016
  • 18
  • 95
  • 150