4

when I execute xargs -n2, enter x x x x x and hit Enter, I get only 4 x back:

$ xargs -n2
x x x x x 
x x 
x x 

But, when I pipe the x x x x x into the same command, I get the same amount of x back:

$ echo x x x x x | xargs -n2
x x 
x x 
x

why is it that in the first scenario the amount of arguments is either rounded up or rounded down ?

terdon
  • 242,166

1 Answers1

5

In the first line, xargs still waits for the second argument or an end of the input. After pressing Ctrl-D xargs continues with the rest and you will see the 5th x as single argument.

This example may explain the behavior:

(echo "x x x x x"; sleep 5; echo "x") | xargs -n2

Output:

x x
x x
x x     # after 5 seconds

After the 6th x in the second echo statement the input stream is finished and xargs finally has the second argument, but until then it waits the 5 seconds.

chaos
  • 48,171
  • Presumably this is because of the -n 2 which forces xargs to expect arguments that are multiples of 2 right? – terdon Mar 24 '15 at 13:39
  • xargs can also continue with one argument if the second argument would exceed a specific size (-s). But, also in that case xargs would wait until that arguemnt came. – chaos Mar 24 '15 at 13:45