To read its input, xargs
uses the read
function (see also the corresponding Linux manpage).
read
reads the next available data from a file descriptor into an area of memory specified by the calling program. As used by xargs
, read
waits until either data is available, or an error occurs, or it determines that no more data will ever be available. It returns respectively a positive integer, -1, or 0 in each of these cases.
To determine when it’s finished reading its input (its standard input, or the input specified by the -a
option), xargs
looks for the specified end-of-file marker (if the -E
option was used), or a 0 return value from read
.
You can see this in action by running
printf '%s ' {1..1024} | strace -e read xargs -s 2048 -x
cat
know to exit when it's reached the tail of the file it's reading, for example? Why is the answer to howxargs
knows when it's reached the end of its stdin any different? – Charles Duffy Nov 25 '18 at 01:33read()
returning 0 indicates EOF. From theread(2)
man page, section RETURN VALUES: If successful, the number of bytes actually read is returned. Upon reading end-of-file, zero is returned. Otherwise, a -1 is returned and the global variable errno is set to indicate the error. – Charles Duffy Nov 25 '18 at 01:39read()
will either actually read some bytes (and return a positive number with the number of bytes read), or fail to read some bytes (and return a negative number that indicates how/why it failed), or hit end-of-file (and return 0). – Charles Duffy Nov 25 '18 at 01:41