.\"
.\" Copyright (c) 2003 Gunnar Ritter
.\"
.\" This software is provided 'as-is', without any express or implied
.\" warranty. In no event will the authors be held liable for any damages
.\" arising from the use of this software.
.\"
.\" Permission is granted to anyone to use this software for any purpose,
.\" including commercial applications, and to alter it and redistribute
.\" it freely, subject to the following restrictions:
.\"
.\" 1. The origin of this software must not be misrepresented; you must not
.\"    claim that you wrote the original software. If you use this software
.\"    in a product, an acknowledgment in the product documentation would be
.\"    appreciated but is not required.
.\"
.\" 2. Altered source versions must be plainly marked as such, and must not be
.\"    misrepresented as being the original software.
.\"
.\" 3. This notice may not be removed or altered from any source distribution.
.\"
.\" Sccsid @(#)xargs.1	1.8 (gritter) 8/14/05
.TH XARGS 1 "8/14/05" "Heirloom Toolchest" "User Commands"
.SH NAME
xargs \- construct argument list(s) and execute command
.SH SYNOPSIS
\fBxargs\fR [\fIflags\fR] [\fIcommand\fR\ [\fIinitial-arguments\fR]]
.SH DESCRIPTION
.I Xargs
executes
.I command
using the given
.I "initial arguments"
followed by arguments read from standard input.
By default,
.I command
is invoked as many times as necessary
using the maximum number of arguments possible
for each invocation.
If no
.I command is given,
.I echo
is used.
.PP
Data read from standard input is split into a list of arguments,
using blanks and newline characters as delimiters.
Special characters are: backslash
.RB ( \e ),
which causes any following character to be interpreted literally,
and single (\') and double (") quotes,
which cause following characters except newline
to be taken literally until the quote character appears again.
.PP
The following option flags are accepted:
.TP 12
\fB\-e\fR[\fIeofstr\fR]
Sets the logical end-of-file delimiter to
.IR eofstr ;
data is read from standard input
until an argument equal to
.I eofstr
is encountered.
If
.I eofstr
is omitted, no logical end-of-file delimiter is used
and data is read from standard input until the physical
end-of-file is reached.
The default logical end-of-file delimiter is
a single underscore character
.RB ( _ ).
.TP 12
\fB\-i\fR[\fIreplstr\fR]
Changes the operation of
.I xargs
such that each line read from standard input
replaces occurrences of
.I replstr
within
the
.IR initial-arguments .
If
.I replstr
is omitted,
.B {}
is default.
.\"No more than five
Any member of
.I initial-arguments
can each contain any number of occurrences of
.IR replstr .
The
.I command
is executed once for each line
until physical end-of-file is reached
or a line consisting exactly of the
logical end-of-file delimiter is read.
Empty lines and leading blank characters are ignored,
other blank characters are kept regardless of quotes.
.TP 12
\fB\-l\fR[\fInumber\fR]
Invokes command for each
.I number
of lines;
1 is default if no
.I number
is specified.
Empty lines are ignored;
if a line ends with an unquoted blank character,
it is not counted.
.TP 12
\fB\-n\fI\ number\fR
Executes command for each
.I number
of arguments read from standard input,
or with fewer arguments if the maximum argument length is exceeded
or fewer than
.I number
arguments remain.
.TP 12
.B \-p
Each resulting command is written to standard error
along with its arguments
before it is executed
and the user is asked whether it should be executed.
If the response is affirmative,
the command is executed;
otherwise, the execution of this command instance is skipped.
.TP 12
\fB\-s\fI\ size\fR
Sets the maximum length of generated command lines to
.IR size .
.TP 12
.B \-t
Trace mode;
each resulting command is written to standard error
along with its arguments
before it is executed.
.TP 12
.B \-x
If execution cannot be done within the limits specified by the
.BR \-n ,
.BR \-L ,
or
.BR \-s
flags,
.I xargs
will terminate.
.PP
The following options have been introduced by X/Open:
.TP 12
\fB\-E\fI\ eofstr\fR
Same as
\fB\-e\fIeofstr\fR.
.TP 12
\fB\-L\fI\ number\fR
Same as
\fB\-l\fInumber\fR.
.TP 12
\fB\-I\fI\ replstr\fR
Same as
\fB\-i\fIreplstr\fR.
.PP
When any instance of
.I command
exits with a status of \-1
or is terminated by a signal,
.I xargs
terminates.
.SH "ENVIRONMENT VARIABLES"
.TP
.BR LANG ", " LC_ALL
See
.IR locale (7).
.TP
.B LC_CTYPE
Determines the mapping of bytes to characters
and the set of blank characters.
.TP
.B PATH
Used to locate
.IR command .
.SH FILES
/dev/tty
Used with the
.B \-p
option.
.SH "SEE ALSO"
find(1),
sh(1),
locale(7)
.SH NOTES
When
.I xargs
is used to read data
generated by the
.I find
or
.I ls
commands and any of the characters special to
.I xargs
appear in file names,
.I command
will be invoked with a wrong set of file names.
To avert this,
.I find
is given a special pathname consisting of two slashes
that marks the beginning of a file name,
and each character is preceded with a backslash:
.RS
.nf
.sp
find .//. \-print | sed \'s/./\e\e&/g\' | awk \'{
        if (NR > 1) {
                printf("%s", line)
                if ($0 !~ /\e\e\e/\e\e\e//)
                        printf("\e\e")
                printf("\en")
        }
        line = $0
}
END {
        print(line)
}\' | xargs \fIcommand\fR
.fi
.sp
.RE
Note that the
.RI ` "\-exec command {} +" '
operand of
.I find
provides a method to achieve similar results
as with
.I xargs
for simple cases.
