Iterating over an array

Arrays and collections / Arrays

numbers=(2 3 5 7 11 13 17)

string=""
for i in "${numbers[@]}"; do
    string+="$i; "
done
#string is "2; 3; 5; 7; 11; 13; 17; "

#printf repeats its format for every argument
fromPrintf=$(printf '%s; ' "${numbers[@]}")
#fromPrintf is "2; 3; 5; 7; 11; 13; 17; "

#the quotes around ${cities[@]} are needed: without them
#an element with a space is split into two words
cities=("New York" "Paris")
quoted=0
for city in "${cities[@]}"; do
    quoted=$((quoted + 1))
done
#quoted is 2
unquoted=0
for city in ${cities[@]}; do
    unquoted=$((unquoted + 1))
done
#unquoted is 3

echo "string is $string"
echo "fromPrintf is $fromPrintf"
echo "quoted is $quoted"
echo "unquoted is $unquoted"