user2405602 user2405602 - 2 months ago 10
Linux Question

Linux: Pass command output as multiple argument to another command

I want to pass each output from a command as multiple argument to a 2nd command, e.g.:

grep "pattern" input


returns:

file1
file2
file3


and I want to copy these outputs, e.g:

cp file1 file1.bac
cp file2 file2.bac
cp file3 file3.bac


how can I do that in one go? Something like:

grep "pattern" input | cp $1 $1.bac


Thanks

Answer

You can use xargs:

grep 'pattern' input | xargs -I% cp % %.bac
Comments