Linux – Looping a command over a list of arguments in Linux

linuxpipe

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?

Best Answer

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