(As this does not answer the question, this is should have been a comment, but is too long - so treat it as a comment).
As an alternative to FreeBSD's xargs you can use GNU Parallel which does not have this limitation. It even supports repeating the context:
seq 10 | parallel -Xj1 echo con{}text
seq 10 | parallel -mj1 echo con{}text
GNU Parallel is a general parallelizer and makes is easy to run jobs in parallel on the same machine or on multiple machines you have ssh access to. It can often replace a for
loop.
If you have 32 different jobs you want to run on 4 CPUs, a straight forward way to parallelize is to run 8 jobs on each CPU:
GNU Parallel instead spawns a new process when one finishes - keeping the CPUs active and thus saving time:
Installation
If GNU Parallel is not packaged for your distribution, you can do a personal installation, which does not require root access. It can be done in 10 seconds by doing this:
(wget -O - pi.dk/3 || curl pi.dk/3/ || fetch -o - http://pi.dk/3) | bash
For other installation options see http://git.savannah.gnu.org/cgit/parallel.git/tree/README
Learn more
See more examples: http://www.gnu.org/software/parallel/man.html
Watch the intro videos: https://www.youtube.com/playlist?list=PL284C9FF2488BC6D1
Walk through the tutorial: http://www.gnu.org/software/parallel/parallel_tutorial.html
Sign up for the email list to get support: https://lists.gnu.org/mailman/listinfo/parallel
According to the POSIX documentation, xargs
should run the given utility with arguments delimited by either spaces or newlines, and this is what happens in the two first examples of yours.
However, when --replace
(or -I
) is used, only newlines will delimit arguments. The remedy is to give xargs
arguments on separate lines:
$ printf '%s\n' a b c | xargs --max-args=1 --replace="{}" echo x "{}" y
x a y
x b y
x c y
Using POSIX options:
printf '%s\n' a b c | xargs -n 1 -I "{}" echo x "{}" y
Here, I give xargs
not one line but three. It takes one line (at most) and executes the utility with that as the argument.
Note also that -n 1
(or --max-args=1
) in the above is not needed as it's the number of replacements made by -I
that determines the number of arguments used:
$ printf '%s\n' a b c | xargs -I "{}" echo x "{}" y
x a y
x b y
x c y
In fact, the Rationale section of the POSIX spec on xargs
says (my emphasis)
The -I
, -L
, and -n
options are mutually-exclusive. Some implementations use the last one specified if more than one is given on a command line; other implementations treat combinations of the options in different ways.
While testing this, I noticed that OpenBSD's version of xargs
will do the the following if -n
and -I
are used together:
$ echo a b c | xargs -n 1 -I "{}" echo x "{}" y
x a y
x b y
x c y
This is different from what GNU coreutils' xargs
does (which produces x a b c y
). This is due to the implementation accepting spaces as argument delimiter with -n
, even though -I
is used. So, don't use -I
and -n
together (it's not needed anyway).
Best Answer
From the
xargs
manual:This means you can escape quotes if the quotes are quoted themselves:
will work but
echo /Place/=\'http://www.google.com\' | xargs echo
will not.