xargs --replace/-I for single arguments

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.


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

You can make use of an extra pipe like this,

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

The intermediate use of xargs 'echos' each letter 'a b c d' individually because of the '-n1' option. This puts each letter on it's own line like this,

echo a b c d | xargs -n1
a
b
c
d 

It's important to understand when using -I (string replacement), xargs implies -L, which executes the utility command (in your case echo) once per line. Also, you cannot use -n with -L as they are mutually exclusive.