这个问题与这个问题类似,但有所不同:
考虑这个带有可能包含空格的字符串元素的数组:
a@W:$ arr=("eins" "zwei" "eins plus zwei" "vier" "fünf")
a@W:$ echo ${#arr[@]} # ok, 5 elements, as expected
5
a@W:$ echo ${arr[3]} # the fourth one, indexing starts at 0
vier
a@W:$ echo ${arr[2]} # the third one, which contains two blancs
eins plus zwei
a@W:$ ar2=${arr[@]:0:3} # I only want the firs three of them
a@W:$ echo ${ar2[@]}
eins zwei eins plus zwei
a@W:$ echo ${#ar2[@]} # but they are all glued together into one element
1
a@W:$
我必须做什么才能防止将它们全部粘在一起?包含空格的字符串“eins plus zwei”应保留为第三个元素。