Linux – xargs –replace/-I for single arguments

bashcommand linelinuxsshxargs

I'm trying to use xargs to run a command for each provided argument, but unfortunately the –replace/-I flag doesn't seem to work properly when conjugated with -n.
It seems that {} will expand into the full list of arguments read from stdin, regardless of the -n option.

Unfortunately all of the examples on the web seem to be for commands (mv, cp, rm) which will take multiple arguments where {} is expanded.

For example, when running:

echo a b c d | xargs -n 1 -I {} echo derp {}

The output is:

derp a b c d

But I expected:

derp a
derp b
derp c
derp d

However, running it without -I {} yields the expected result:

echo a b c d | xargs -n 1 echo derp
derp a
derp b
derp c
derp d

Is there any way to achieve this with xargs?
My ultimate intention is to use it to run multiple (parralel) ssh sessions, like

echo server{1..90} | xargs -n 1 -P 0 -I {} ssh {} 'echo $SOME_HOST_INFO'

I'm running xargs (GNU findutils) 4.4.2 on RHEL 6.3.

Best Answer

You can echo with newlines to achieve your expected result. In your case with the server expansion that would be:

$ echo -e server{1..4}"\n" | xargs -I{} echo derp {}
derp server1
derp server2
derp server3
derp server4
Related Question