This question is similar to this one but it differs from it:
Consider this array with string elements which may contain spaces:
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:$
What mus I do to prevent this gluing them all together? The string containing spaces, "eins plus zwei" shall stay the third element.
printf '<%s>' "${arr[@]:0:3}"
They are treated as seperate elements. If you want to "glue" them into 1 element then you'd useprintf '<%s>' "${arr[*]:0:3}"
. Does this help?${#ar2[@]}
shows the array has one element