i=0
while [ $i -lt ${#array[*]} ]; do
echo ${array[$i]} is in ${array2[$i]}
i=$(( $i + 1));
done
Vietnam is in Asia
Germany is in Europe
Argentina is in America
EDIT: Do not use the below tr based implementation. It will not work for array elements containing spaces. Not removing it so as to keep the comments relevant. See glenn jackman's comment instead of below answer.
编辑
Alternately, you can use this option (without loop):
paste <(tr ' ' '\n' <<< ${array[*]}) <(tr ' ' '\n' <<< ${array2[*]}) | sed 's/\t/ is in /'
From anishsane's answer and the comments therein we now know what you want. Here's the same thing in a 巴希尔 style, using a for loop. See the Looping Constructs section in the reference manual. I'm also using printf instead of echo.
#!/bin/bash
array=( "Vietnam" "Germany" "Argentina" )
array2=( "Asia" "Europe" "America" )
for i in "${!array[@]}"; do
printf "%s is in %s\n" "${array[i]}" "${array2[i]}"
done
另一种可能性是使用关联数组:
#!/bin/bash
declare -A continent
continent[Vietnam]=Asia
continent[Germany]=Europe
continent[Argentina]=America
for c in "${!continent[@]}"; do
printf "%s is in %s\n" "$c" "${continent[$c]}"
done
array=(
Vietnam
Germany
Argentina
)
array2=(
Asia
Europe
America
)
for index in ${!array[*]}; do
echo "${array[$index]} is in ${array2[$index]}"
done
Vietnam is in Asia
Germany is in Europe
Argentina is in America