关于bash shell脚本中的数组

时间:2022-02-09 19:25:44

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.

将显示1 2 3或4 5 6。

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?

显示的值并不像我预期的那样....任何人都可以给我正确的使用方法吗?

1 个解决方案

#1


4  

Quotation is what you need:

报价是你需要的:

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

#1


4  

Quotation is what you need:

报价是你需要的:

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