Another way, which is also portable between Bourne shells (sh, bash, zsh, etc.):
things="one two"
for one_thing in $(echo $things); do
echo $one_thing
done
Or, if you don't need $things
defined as a variable:
for one_thing in one two; do
echo $one_thing
done
Using for x in y z
will instruct the shell to loop through a list of words, y, z
.
The first example uses command substitution to transform the string "one two"
into a list of words, one two
(no quotes).
The second example is the same thing without echo
.
Here's an example that doesn't work, to understand it better:
for one_thing in "one two"; do
echo $one_thing
done
Notice the quotes. This will simply print
one two
because the quotes mean the list has a single item, one two
.