这很接近,但我在循环中引用第二个数组时遇到了问题。语法应该是什么样的? $ item返回“1”而不是“a,b,c或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
答案 0 :(得分:5)
您可以这样做:
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
运行:
$ ./mkt.sh
color: teal
-- a
-- b
-- c
-- d
color: purple
-- x
-- y
-- z
color: pink
color: red
color: green
color: darkblue
color: skyblue
$
现金: