Why does for loop return only the first entry from my lists?

mickt
mickt used Ask the Experts™
on
Why does for loop return only the first entry from my lists?  Stripped down script as follows.

groups=(baremetal production staging)
baremetal=(test1 test2)
production=(test3)
staging=()

for group in ${groups[@]}
  do
        echo $group

    for host in ${!group}   # only adds the 1st
      do
           echo $host

      done
  done


./test.sh
baremetal
test1
production
test3
staging
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
See a similar case here

This sould do the trick:

groups=(baremetal production staging)
baremetal=(test1 test2)
production=(test3)
staging=()

for group in ${groups[@]}
  do
        echo $group:

    for host in $(eval echo \${$group[*]})
      do
           echo -$host

      done
  done

Open in new window

Author

Commented:
Cheers!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial