在循环中的两个不同阵列之间建立关系



我创建了两个数组:

1st数组:

echo "${array1[@]}"
two
three
four
five

第二个数组:

echo "${array2[@]}"
apples
carrots
potatoes
tomatoes

我想将它们结合在循环中(或类似的东西)中,并保持第一个数组与第二个数组之间的关系。

two apples
three carrots
four potatoes
five tomatoes

谢谢。

将两个平行阵列拉链,在索引上迭代。(假设两个数组都使用相同的索引,几乎总是如此。)

for i in "${!array1[@]}"; do
  new_array+=( "${array1[i]} ${array2[i]}" )
done