It seems the -n option on the xargs found in SmartOS (and I assume Solaris) does not behave like any other version of xargs I've encountered.

Take this example:

Builtin /usr/bin/xargs (odd behavoir):

# printf 'one\0two\0three' | xargs -0 -I{} -n 1 echo "- {}"
- {} one
- {} two
- {} three

GNU Findutils /opt/local/bin/xargs (expected behavoir):

# printf 'one\0two\0three' | /opt/local/bin/xargs -0 -I{} -n 1 echo "- {}"
- one
- two
- three

Xargs from MacOS, NetBSD, and CentOS all behave the same as the last example. What's different about the SmartOS xargs?

From the SmartOS xargs manpage:

   -n number
                  Invokes utility using as many standard input arguments
                  as possible, up to number (a positive decimal integer)
                  arguments maximum. Fewer arguments are used if:

                      o      The command line length accumulated exceeds
                             the size specified by the -s option (or
                             {LINE_MAX} if there is no -s option), or

                      o      The last iteration has fewer than number, but
                             not zero, operands remaining.

From the Gnu Findutils xargs manpage:

   -n max-args, --max-args=max-args
          Use at most max-args arguments per command line.  Fewer than max-args arguments will be used if the size (see the -s option) is  exceeded,  un‐
          less the -x option is given, in which case xargs will exit.

I discovered this difference while porting a shell script and I'm curious if anyone knows why the behavoir is different.

  • 1
    Looks like it's the -I that's behaving differently (not doing anything on SnartOS), not the -n. – n.st Nov 30 '18 at 5:15
  • That was my thought as well, but if I remove -n 1 it works as expected. I've tried other characters besides {} as well as the -i flag which assumes {} I won't rule out the -I but I don't know how else to try it. – functionvoid Nov 30 '18 at 5:28
  • What happens with those other placeholders? – n.st Nov 30 '18 at 5:30
  • If you want to see that GNU xargs behavior is not always the one expected, try this printf 'one\0two\0three\0four' | gxargs -0 -I{} -n 2 echo "- {}" ;-) – mosvy Nov 30 '18 at 5:42
  • I'm surprised that -0 worked with the OS-supplied xargs. -0 is a non-standard GNU extension to xargs. It's not a POSIX-standard option. – Andrew Henle Nov 30 '18 at 10:42

You cannot combine the -I and -n options. That's what the standard says:

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.

See also this and this.

|improve this answer|||||
  • Your answer would be more complete by indicating which are the correct arguments that should be used in this particular case... Should the -n or the -I be dropped, and why? – filbranden Nov 30 '18 at 5:57
  • 3
    @FilipeBrandenburger I guess the OP's example was only to illustrate the broken buggy behavior; I've already explained in the linked answer how to insert arguments in the xargs' command at any position without the use of -I. I will not repeat it here, since that wasn't the point of the question. – mosvy Nov 30 '18 at 6:53

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.