Simple Unix way of looping through space-delimited strings?

The easiest way to do it is a classic trick that's been in the bourne shell for a while.

for filename in `cat file_list`; do
  # Do stuff here
done

You can change the file to have words be line separated instead of space separated. This way, you can use the typical syntax:

while read line
do
   do things with $line
done < file

With tr ' ' '\n' < file you replace spaces with new lines, so that this should make:

while read line
do
   do things with $line
done < <(tr ' ' '\n' < file)

Test

$ cat a
hello this is a set of strings
$ while read line; do echo "line --> $line"; done < <(tr ' ' '\n' < a)
line --> hello
line --> this
line --> is
line --> a
line --> set
line --> of
line --> strings

Tags:

Unix

Bash

Sh