bashxargs

Is there any benefit of executing "| xargs" without any arguments?


I came across scripts where | xargs is executed without any arguments. I started removing such occurrences because scripts just work without them but wanted to verify that I'm not missing something. Perhaps xargs defaults to sanitising some characters, etc.

... | cut -d: -f2- | xargs

Is there any point of having the | xargs above? Thanks in advance.


Solution

  • Without any arguments xargs will combine all lines in the output into a single line, and will remove extra spaces:

    $ echo $'a   b\nb'
    a   b
    b
    
    $ echo $'a   b\nb' | xargs
    a b b
    

    Note that this will only work if the input of xargs is smaller than the maximum size of the command buffer. See example below:

    $ seq 100 | xargs | wc -l
    1
    $ seq 1000000 | xargs | wc -l
    53