6

This works as expected:

$ echo a b c | xargs --replace="{}" echo x "{}" y
x a b c y

This does too:

$ echo a b c | xargs --max-args=1 echo x
x a
x b
x c

But this doesn't work as expected:

$ echo a b c | xargs --max-args=1 --replace="{}" echo x "{}" y
x a b c y

And neither does this:

$ echo a b c | xargs --delimiter=' ' --max-args=1 --replace="{}" echo x "{}" y
x a y
x b y
x c
 y

I expected this output:

x a y
x b y
x c y

As a workaround, I am using printf and two xargs, but that is ugly:

$ echo a b c | xargs printf '%s\0' | \
> xargs --null --max-args=1 --replace="{}" echo x "{}" y
x a y
x b y
x c y

Any idea why this is happening?

5

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).

  • 1
    That behaviour (that newline should be the only delimiter with -I) is documented in the GNU xargs documentation and the POSIX specification (under XSI, so it's not surprising that OpenBSD doesn't comply there). In any case, using -n with -I makes little sense as {} is to replace one argument anyway. – Stéphane Chazelas Mar 5 '18 at 16:35
  • @StéphaneChazelas Ah. I was looking too closely at -n and not at -I. Will update. – Kusalananda Mar 5 '18 at 16:41
  • @StéphaneChazelas It seems that OpenBSD's xargs does the correct thing if -I and -n is not mixed. I rewrote that part of the answer. – Kusalananda Mar 5 '18 at 16:46
  • Thanks for the pointer to the POSIX xargs. The way I read it, it's nearly useless, as it says that -I, -L and -n are mutually exclusive. Glad we have the GNU version. // Actually echo a b c was a placeholder for any command that generates multiple space-separated parameters, hence why I used | xargs printf instead of using printf directly. // I'm still digesting your post. You've written that OpenBSD xargs will do the wrong thing, is that a typo? – Pedro Gimeno Mar 5 '18 at 17:16
  • 1
    @PedroGimeno No typo. It's the wrong output since it uses the three letters as separate arguments even though -I is used. It does, however, do the right thing if -n is not used. As you say, -I and -n are mutually exclusive, and an implementation may choose to handle this situation in any way it wants, so "wrong" is maybe the wrong word... – Kusalananda Mar 5 '18 at 17:20
0

One work around I found was feeding the output of --max-args to --replace like this:

echo a b c | xargs -n 1 | xargs -i echo firstPart {} secondPart

This will output.

firstPart a secondPart
firstPart b secondPart
firstPart b secondPart

I guess it's basically looping through the input with --max-args and adding the new line character by default.

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.