xargs Command – Does Order of Options Matter

xargs

Input 0

echo foo | xargs -L 1 -I '{}' echo '{}'

Output 0

foo

Input 1

echo foo | xargs -I '{}' -L 1 echo '{}'

Output 1

{} foo

Why changing the order of options of xargs changes the output?

Version: xargs (GNU findutils) 4.6.0

Best Answer

When options given to xargs conflict, order may matter.

IEEE Std 1003.1-2008, 2016 Edition/Open Group Base Specifications Issue 7 added the following text1 to the specification of xargs:

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.

This codifies the behavior of many implementations of xargs, going back to the original version in PWB/Unix, whose man page says

When there are flag conflicts (e.g., -l vs. -n), the last flag has precedence.

In the GNU version of xargs, -L disables any previous -I option. So in your second example,

echo foo | xargs -I '{}' -L 1 echo '{}'

{} is just an ordinary argument passed to echo, with no substitution being done.

[1]Compared to IEEE Std 1003.1, 2004 Edition/Open Group Base Specifications Issue 6.

Related Question