I'd like to explain the above in more detail, if I may.
The syntax of the basic
for loop is this:
Code:
for variable in <wordlist> ; do
commands
done
If the <wordlist> includes variables,
brace expansions,
command substitutions, or in
globbing patterns, as in this case, they get expanded into their final values before the loop is run. This means you can include as many different entries as you want after the "in". What matters to the loop is how the list looks after expansion.
Code:
$ echo 'foo1 bar1 baz1' >testfile.txt
$ varA='foo2 bar2 baz2'
$ arrB=( foo3 bar3 baz3 )
$ touch {foo,bar,baz}4
$ for var in $(cat testfile.txt) $varA "${arrB[@]}" *4 ; do echo "$var" ; done
foo1
bar1
baz1
foo2
bar2
baz2
foo3
bar3
baz3
bar4
baz4
foo4
$ rm *4 testfile.txt
$ unset varA arrB
However, in spite of what I just posted, you should generally
not read lines from text files or commands with for. Use a
while+read loop for that. Only globbing patterns are completely safe from word-splitting effects.
Notice also that I did not quote the
$(..) or the
$varA, as I wanted the contents to be split into the individual words. Quotes would have caused the contents to be treated as a
single value. Generally, it's much better to use an
array for lists of words, as my use of
arrB demonstrates.
Code:
$ for var in "$(cat testfile.txt)" "$varA" "${arrB[@]}" "*4" ; do echo "$var" ; done
foo1 bar1 baz1
foo2 bar2 baz2
foo3
bar3
baz3
*4