Given an application, myApplication, that streams lines to stdout

$ myApplication

How do I use xargs to pass those values as flags to a command instead of passing them as parameters?

For example, the resulting invocations should be:

anotherApplication --flag "flag1" parameterNotFromXargs
anotherApplication --flag "flag2" parameterNotFromXargs
anotherApplication --flag "flag3" parameterNotFromXargs

My failed attempt using the -I option produced the wrong output:

$ myApplication | xargs -L1 -I % echo "e: %"
e: %
e: %
e: %
  • 1
    What OS are you running? What version of xargs? – ilkkachu Sep 13 '18 at 17:08

Unable to replicate:

$ echo -e "one\ntwo\nthree" | xargs -L1 -I% echo "I was told % times"
I was told one times
I was told two times
I was told three times

If myApplication is returning its output on standard input, the above invocation of xargs should work properly. If the output you are seeing is actually standard error, you need to redirect that to standard output with myApplication 2>&1.


The easiest way is a script

anotherApplication --flag "$1" parameterNotFromXargs

And call it with

myApplication | xargs -L1 ./myscript.sh

If you want to avoid the script, you can use give the command inline:

myApplication | xargs -L1 sh -c 'anotherApplication --flag "$1" parameterNotFromXargs' _

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.