Could you please tell me how to iterate over list where items can contain whitespaces?
x=("some word", "other word", "third word")
for word in $x ; do
echo -e "$word\n"
done
how to force it to output:
some word
other word
third word
instead of:
some
word
(...)
third
word
To loop through items properly you need to use
${var[@]}
. And you need to quote it to make sure that the items with spaces are not split:"${var[@]}"
.All together:
Or, saner (thanks Charles Duffy) with
printf
:Two possible solutions, one similar to fedorqui's solution, without the extra ',', and another using array indexing:
Output:
edit: fixed issues with the indexed solution as pointed out by cravoori