The regular case with linefeeds, xargs
calls printf
and something is printed:
>>printf "foo\n" | xargs -r printf "->%s\n"
->foo
>>
With an empty input instead, -r
makes xargs
not call the command at all:
>>printf "\n" | xargs -r printf "->%s\n"
>>
Using nulls instead of line feeds, everything is the same if there is an input to xargs
:
>>printf "foo\0" | xargs -r -0 printf "->%s\n"
->foo
>>
But if there is no input, something is printed anyway:
>>printf "\0" | xargs -r -0 printf "->%s\n"
->
>>
So, either
-
I'm missing something (what?)
-
There is some good reason to ignore
-r
when used with-0
(but which)? -
There is a bug in my
xargs
(findutils 4.6.0.225-235f) but it's hard to decide:- I find the same behavior in two other instances (a 4.7.0-git on Ubuntu 16.04 and another 4.6.0 on Windows),
- I can't believe this kind of thing could slip thru regression tests.
- On the other hand this question implies that it could have worked in the past
So, what is the true expected behavior of xargs
?
Best Answer
xargs -r cmd
skips runningcmd
without arguments.With
-0
, one argument is created for each NUL-delimited record on inputYou're feeding one empty record, so
cmd
is called with one empty argument (and withprintf '\0\0'
, that would be 2 empty records, etc).With:
You're feeding no record, so
cmd
is not run.With:
You're still not feeding any record, but because
-r
is not provided,cmd
is still called once without argument.Without
-0
,xargs
expects a very special input format, it's not a simple delimited list like withxargs -0
orxargs -d '\n'
To feed one empty argument, you need something like:
or for that matters:
printf -- '-%s\n'
is not the best choice of command to test that as it gives the same output when passed no argument as when passed one empty argument.A better one could be:
Or: