Hao Shen
Hao Shen

Reputation: 2735

About the array in bash shell script

Just a simple question.

I have an array:

array=("1 2 3" "4 5 6")

If I do:

echo ${array[0]}
echo ${array[1]}

1 2 3 or 4 5 6 will be shown.

However, if I do:

for iter in ${array[@]}
do
echo $iter
done

The shown value is not as I expected.... Can anyone give me the right way to use it?

Upvotes: 1

Views: 101

Answers (1)

jaypal singh
jaypal singh

Reputation: 77085

Quotation is what you need:

for iter in "${array[@]}"; do 
  echo "$iter"
done

Upvotes: 4

Related Questions