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?
You can use xargs with -n1 to run a command once for each piped argument
In the case of touch however which accepts multiple arguments
will probably work for you.
I only use for ... do ... done for very simple cases.
For more complicated/dangerous scenarios:
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):If the filenames contain whitespace, then the following will work around them:
This will create files named:
file name 1
file name 2
named file 3
etc
Assuming, of course, that it does produce those names.
"some_command | xargs touch" will probably do the trick, however there are two pitfalls: