In bash, just use space as delimiter (read -d ' '
). This method requires some preprocessing to translate newlines into spaces (using tr
) and to merge several spaces into a single one (using sed
):
{
tr '\n' ' ' | sed 's/ */ /g' | while read -d ' ' WORD
do
echo -n "<${WORD}> "
done
echo
} << EOF
Here you have some words, including * wildcards
that don't get expanded,
multiple spaces between words,
and lines with spaces at the begining.
EOF
The main advantage of this method is that you don't need to worry about the array syntax and just work as with a for
loop, but without wildcard expansion.