Looping a command over a list of arguments in Linux

How to execute the same command with arguments that are delivered by another command via pipe?

As a result of the extraction of file names from a source I get:

    $some_command
    filename1
    filename2
    filename3
    ...
    filenameN

I'd like to create files with these filenames with touch. How can I loop touch over these names?


I only use for ... do ... done for very simple cases.

For more complicated/dangerous scenarios:

command | sed 's/^/touch /'

This does nothing but prints intended commands. Review the results, then perform the same thing piping to sh -x (the -x flag is for debugging):

command | sed 's/^/touch /' | sh -x

You can use xargs with -n1 to run a command once for each piped argument

$some_command | xargs -n 1 touch

In the case of touch however which accepts multiple arguments

touch `$some_command` 

will probably work for you.


for i in `$some_command`; do touch $i; done

If the filenames contain whitespace, then the following will work around them:

some_command | while read filename ; do touch "$filename" ; done

This will create files named:
file name 1
file name 2
named file 3
etc

Assuming, of course, that it does produce those names.