1

This is close, but I'm having trouble referencing the second array in my loop. What should the syntax look like? $item is returning "1" instead of "a,b,c or d"

colors=( teal purple pink red green darkblue skyblue )
teal=( a b c d )


for color in ${colors[@]}
do
    echo $color

    for item in ${#{$color}[@]}
    do
        echo $item
    done

done

1 Answer 1

5

You can do something like this:

mkt.sh

colors=( teal purple pink red green darkblue skyblue )
teal=( a b c d )
purple=( x y z )

for color in ${colors[@]}
do
    echo color: $color

    declare -a 'vals=("${'"$color"'[@]}")'
    for item in ${vals[@]}
    do
        echo "-- $item"
    done
done

Running:

$ ./mkt.sh 
color: teal
-- a
-- b
-- c
-- d
color: purple
-- x
-- y
-- z
color: pink
color: red
color: green
color: darkblue
color: skyblue
$ 

Credits:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.