I stumbled on a similar problem and found a solution which I think is nicer and cleaner than those presented so far.
The syntax for xargs
that I have ended with would be (for your example):
xargs -I X echo --file=X
with a full command line being:
my-program $(cat input.txt | xargs -I X echo --file=X)
which will work as if
my-program --file=a.txt --file=b.txt --file=c.txt
was done (providing input.txt
contains data from your example).
Actually, in my case I needed to first find the files and also needed them sorted so my command line looks like this:
my-program $(find base/path -name "some*pattern" -print0 | sort -z | xargs -0 -I X echo --files=X)
Few details that might not be clear (they were not for me):
some*pattern
must be quoted since otherwise shell would expand it before passing to find
.
-print0
, then -z
and finally -0
use null-separation to ensure proper handling of files with spaces or other wired names.
Note however that I didn't test it deeply yet. Though it seems to be working.
seq 1996 2022 | xargs
calls for this when I'm trying to stripe my files by year on my file system.