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
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0