Search code examples
linuxshellunix

How to pass command output as multiple arguments to another command


I want to pass each output from a command as multiple argument to a second 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

Solution

  • You can use xargs:

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