Why does xargs -L yield the right format, while xargs -n doesn't? Why does xargs -L yield the right format, while xargs -n doesn't? unix unix

Why does xargs -L yield the right format, while xargs -n doesn't?


-L splits by lines; echo doesn't separate its output by lines but by spaces, so a single ls -l is run and that formats all the columns as a group.

-n splits by parameters; in the absence of -L or -0, the separator is whitespace (possibly modified by quoting), so each filename gets its own ls -l run and there is no way for the independent runs to coordinate column widths.


The POSIX standard mandates:

-L number

The utility shall be executed for each non-empty number lines of arguments from standard input. The last invocation of utility shall be with fewer lines of arguments if fewer than number remain. A line is considered to end with the first unless the last character of the line is a <blank>; a trailing <blank> signals continuation to the next non-empty line, inclusive.

-n number

Invoke utility using as many standard input arguments as possible, up to number (a positive decimal integer) arguments maximum.

(Emphasis added.) Since echo * produces a single line, xargs -L 1 just feeds all of the filenames to ls at once, and only then can ls nicely align the columns.

(In other words, your first command is equivalent ls -l index.html*, except that it doesn't handle filenames containing blanks correctly.)


Minimal runnable example of how -L splits by lines and -n by whitespace

This should clarify what geekosaur said further:

printf '1 2\n3 4\n' | xargs -L1 echo

splits by line and therefore is equivalent to:

echo 1 2echo 3 4

which outputs:

1 23 4

However:

printf '1 2\n3 4\n' | xargs -n1 echo

splits on any whitespace, and is therefore equivalent to:

echo 1echo 2echo 3echo 4

and produces instead:

1234

Portability

As mentioned at https://unix.stackexchange.com/questions/448290/are-l1-and-n-1-the-same-for-xargs/448379#448379 -L is slightly less portable as it is a XSI POSIX extension, while -n isn't.