Problem using xargs --max-args --replace with default delimiter

Clash Royale CLAN TAG#URR8PPP
up vote
3
down vote
favorite
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' |
> xargs --null --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Any idea why this is happening?
linux xargs gnu
add a comment |Â
up vote
3
down vote
favorite
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' |
> xargs --null --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Any idea why this is happening?
linux xargs gnu
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
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' |
> xargs --null --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Any idea why this is happening?
linux xargs gnu
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' |
> xargs --null --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Any idea why this is happening?
linux xargs gnu
edited Mar 5 at 17:02
asked Mar 5 at 16:04
Pedro Gimeno
1304
1304
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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 '%sn' a b c | xargs --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Using POSIX options:
printf '%sn' 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 '%sn' 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-noptions 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 GNUxargsdocumentation and the POSIX specification (under XSI, so it's not surprising that OpenBSD doesn't comply there). In any case, using-nwith-Imakes little sense asis to replace one argument anyway.
â Stéphane Chazelas
Mar 5 at 16:35
@StéphaneChazelas Ah. I was looking too closely at-nand not at-I. Will update.
â Kusalananda
Mar 5 at 16:41
@StéphaneChazelas It seems that OpenBSD'sxargsdoes the correct thing if-Iand-nis not mixed. I rewrote that part of the answer.
â Kusalananda
Mar 5 at 16:46
Thanks for the pointer to the POSIXxargs. The way I read it, it's nearly useless, as it says that-I,-Land-nare mutually exclusive. Glad we have the GNU version. // Actuallyecho a b cwas a placeholder for any command that generates multiple space-separated parameters, hence why I used| xargs printfinstead of usingprintfdirectly. // I'm still digesting your post. You've written that OpenBSDxargswill do the wrong thing, is that a typo?
â Pedro Gimeno
Mar 5 at 17:16
1
@PedroGimeno No typo. It's the wrong output since it uses the three letters as separate arguments even though-Iis used. It does, however, do the right thing if-nis not used. As you say,-Iand-nare 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 at 17:20
 |Â
show 4 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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 '%sn' a b c | xargs --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Using POSIX options:
printf '%sn' 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 '%sn' 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-noptions 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 GNUxargsdocumentation and the POSIX specification (under XSI, so it's not surprising that OpenBSD doesn't comply there). In any case, using-nwith-Imakes little sense asis to replace one argument anyway.
â Stéphane Chazelas
Mar 5 at 16:35
@StéphaneChazelas Ah. I was looking too closely at-nand not at-I. Will update.
â Kusalananda
Mar 5 at 16:41
@StéphaneChazelas It seems that OpenBSD'sxargsdoes the correct thing if-Iand-nis not mixed. I rewrote that part of the answer.
â Kusalananda
Mar 5 at 16:46
Thanks for the pointer to the POSIXxargs. The way I read it, it's nearly useless, as it says that-I,-Land-nare mutually exclusive. Glad we have the GNU version. // Actuallyecho a b cwas a placeholder for any command that generates multiple space-separated parameters, hence why I used| xargs printfinstead of usingprintfdirectly. // I'm still digesting your post. You've written that OpenBSDxargswill do the wrong thing, is that a typo?
â Pedro Gimeno
Mar 5 at 17:16
1
@PedroGimeno No typo. It's the wrong output since it uses the three letters as separate arguments even though-Iis used. It does, however, do the right thing if-nis not used. As you say,-Iand-nare 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 at 17:20
 |Â
show 4 more comments
up vote
2
down vote
accepted
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 '%sn' a b c | xargs --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Using POSIX options:
printf '%sn' 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 '%sn' 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-noptions 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 GNUxargsdocumentation and the POSIX specification (under XSI, so it's not surprising that OpenBSD doesn't comply there). In any case, using-nwith-Imakes little sense asis to replace one argument anyway.
â Stéphane Chazelas
Mar 5 at 16:35
@StéphaneChazelas Ah. I was looking too closely at-nand not at-I. Will update.
â Kusalananda
Mar 5 at 16:41
@StéphaneChazelas It seems that OpenBSD'sxargsdoes the correct thing if-Iand-nis not mixed. I rewrote that part of the answer.
â Kusalananda
Mar 5 at 16:46
Thanks for the pointer to the POSIXxargs. The way I read it, it's nearly useless, as it says that-I,-Land-nare mutually exclusive. Glad we have the GNU version. // Actuallyecho a b cwas a placeholder for any command that generates multiple space-separated parameters, hence why I used| xargs printfinstead of usingprintfdirectly. // I'm still digesting your post. You've written that OpenBSDxargswill do the wrong thing, is that a typo?
â Pedro Gimeno
Mar 5 at 17:16
1
@PedroGimeno No typo. It's the wrong output since it uses the three letters as separate arguments even though-Iis used. It does, however, do the right thing if-nis not used. As you say,-Iand-nare 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 at 17:20
 |Â
show 4 more comments
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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 '%sn' a b c | xargs --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Using POSIX options:
printf '%sn' 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 '%sn' 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-noptions 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).
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 '%sn' a b c | xargs --max-args=1 --replace="" echo x "" y
x a y
x b y
x c y
Using POSIX options:
printf '%sn' 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 '%sn' 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-noptions 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).
edited Mar 5 at 17:23
answered Mar 5 at 16:09
Kusalananda
103k13202318
103k13202318
1
That behaviour (that newline should be the only delimiter with -I) is documented in the GNUxargsdocumentation and the POSIX specification (under XSI, so it's not surprising that OpenBSD doesn't comply there). In any case, using-nwith-Imakes little sense asis to replace one argument anyway.
â Stéphane Chazelas
Mar 5 at 16:35
@StéphaneChazelas Ah. I was looking too closely at-nand not at-I. Will update.
â Kusalananda
Mar 5 at 16:41
@StéphaneChazelas It seems that OpenBSD'sxargsdoes the correct thing if-Iand-nis not mixed. I rewrote that part of the answer.
â Kusalananda
Mar 5 at 16:46
Thanks for the pointer to the POSIXxargs. The way I read it, it's nearly useless, as it says that-I,-Land-nare mutually exclusive. Glad we have the GNU version. // Actuallyecho a b cwas a placeholder for any command that generates multiple space-separated parameters, hence why I used| xargs printfinstead of usingprintfdirectly. // I'm still digesting your post. You've written that OpenBSDxargswill do the wrong thing, is that a typo?
â Pedro Gimeno
Mar 5 at 17:16
1
@PedroGimeno No typo. It's the wrong output since it uses the three letters as separate arguments even though-Iis used. It does, however, do the right thing if-nis not used. As you say,-Iand-nare 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 at 17:20
 |Â
show 4 more comments
1
That behaviour (that newline should be the only delimiter with -I) is documented in the GNUxargsdocumentation and the POSIX specification (under XSI, so it's not surprising that OpenBSD doesn't comply there). In any case, using-nwith-Imakes little sense asis to replace one argument anyway.
â Stéphane Chazelas
Mar 5 at 16:35
@StéphaneChazelas Ah. I was looking too closely at-nand not at-I. Will update.
â Kusalananda
Mar 5 at 16:41
@StéphaneChazelas It seems that OpenBSD'sxargsdoes the correct thing if-Iand-nis not mixed. I rewrote that part of the answer.
â Kusalananda
Mar 5 at 16:46
Thanks for the pointer to the POSIXxargs. The way I read it, it's nearly useless, as it says that-I,-Land-nare mutually exclusive. Glad we have the GNU version. // Actuallyecho a b cwas a placeholder for any command that generates multiple space-separated parameters, hence why I used| xargs printfinstead of usingprintfdirectly. // I'm still digesting your post. You've written that OpenBSDxargswill do the wrong thing, is that a typo?
â Pedro Gimeno
Mar 5 at 17:16
1
@PedroGimeno No typo. It's the wrong output since it uses the three letters as separate arguments even though-Iis used. It does, however, do the right thing if-nis not used. As you say,-Iand-nare 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 at 17:20
1
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 at 16:35
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 at 16:35
@StéphaneChazelas Ah. I was looking too closely at
-n and not at -I. Will update.â Kusalananda
Mar 5 at 16:41
@StéphaneChazelas Ah. I was looking too closely at
-n and not at -I. Will update.â Kusalananda
Mar 5 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 at 16:46
@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 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 at 17:16
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 at 17:16
1
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 at 17:20
@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 at 17:20
 |Â
show 4 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f428310%2fproblem-using-xargs-max-args-replace-with-default-delimiter%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password