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

The name of the pictureThe name of the pictureThe name of the pictureClash 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?







share|improve this question


























    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?







    share|improve this question
























      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?







      share|improve this question














      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?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 5 at 17:02

























      asked Mar 5 at 16:04









      Pedro Gimeno

      1304




      1304




















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






          share|improve this answer


















          • 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











          • @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











          • 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




            @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










          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          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






























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






          share|improve this answer


















          • 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











          • @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











          • 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




            @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














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






          share|improve this answer


















          • 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











          • @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











          • 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




            @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












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






          share|improve this answer














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







          share|improve this answer














          share|improve this answer



          share|improve this answer








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







          • 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












          • 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











          • @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











          • 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




            @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







          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












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay