Blame man-pages-posix-2013-a/man1p/xargs.1p

Packit 7cfc04
'\" et
Packit 7cfc04
.TH XARGS "1P" 2013 "IEEE/The Open Group" "POSIX Programmer's Manual"
Packit 7cfc04
.SH PROLOG
Packit 7cfc04
This manual page is part of the POSIX Programmer's Manual.
Packit 7cfc04
The Linux implementation of this interface may differ (consult
Packit 7cfc04
the corresponding Linux manual page for details of Linux behavior),
Packit 7cfc04
or the interface may not be implemented on Linux.
Packit 7cfc04
Packit 7cfc04
.SH NAME
Packit 7cfc04
xargs
Packit 7cfc04
\(em construct argument lists and invoke utility
Packit 7cfc04
.SH SYNOPSIS
Packit 7cfc04
.LP
Packit 7cfc04
.nf
Packit 7cfc04
xargs \fB[\fR\(miptx\fB] [\fR\(miE \fIeofstr\fB] [\fR\(miI \fIreplstr\fR|\(miL \fInumber\fR|\(min \fInumber\fB]\fR
Packit 7cfc04
    \fB[\fR\(mis \fIsize\fB] [\fIutility \fB[\fIargument\fR...\fB]]\fR
Packit 7cfc04
.fi
Packit 7cfc04
.SH DESCRIPTION
Packit 7cfc04
The
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility shall construct a command line consisting of the
Packit 7cfc04
.IR utility
Packit 7cfc04
and
Packit 7cfc04
.IR argument
Packit 7cfc04
operands specified followed by as many arguments read in sequence from
Packit 7cfc04
standard input as fit in length and number constraints specified by the
Packit 7cfc04
options. The
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility shall then invoke the constructed command line and wait for its
Packit 7cfc04
completion. This sequence shall be repeated until one of the following
Packit 7cfc04
occurs:
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
An end-of-file condition is detected on standard input.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
An argument consisting of just the logical end-of-file string
Packit 7cfc04
(see the
Packit 7cfc04
.BR \(miE
Packit 7cfc04
.IR eofstr
Packit 7cfc04
option) is found on standard input after double-quote processing,
Packit 7cfc04
<apostrophe>
Packit 7cfc04
processing, and
Packit 7cfc04
<backslash>-escape
Packit 7cfc04
processing (see next paragraph). All arguments up to but not including
Packit 7cfc04
the argument consisting of just the logical end-of-file string shall be
Packit 7cfc04
used as arguments in constructed command lines.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
An invocation of a constructed command line returns an exit status of
Packit 7cfc04
255.
Packit 7cfc04
.P
Packit 7cfc04
The application shall ensure that arguments in the standard input are
Packit 7cfc04
separated by unquoted
Packit 7cfc04
<blank>
Packit 7cfc04
characters, unescaped
Packit 7cfc04
<blank>
Packit 7cfc04
characters, or
Packit 7cfc04
<newline>
Packit 7cfc04
characters. A string of zero or more non-double-quote (\c
Packit 7cfc04
.BR '\&"' )
Packit 7cfc04
characters and non-\c
Packit 7cfc04
<newline>
Packit 7cfc04
characters can be quoted by enclosing them in double-quotes. A string
Packit 7cfc04
of zero or more non-\c
Packit 7cfc04
<apostrophe>
Packit 7cfc04
(\c
Packit 7cfc04
.BR '\e'' )
Packit 7cfc04
characters and non-\c
Packit 7cfc04
<newline>
Packit 7cfc04
characters can be quoted by enclosing them in
Packit 7cfc04
<apostrophe>
Packit 7cfc04
characters. Any unquoted character can be escaped by preceding it with a
Packit 7cfc04
<backslash>.
Packit 7cfc04
The utility named by
Packit 7cfc04
.IR utility
Packit 7cfc04
shall be executed one or more times until the end-of-file is reached or
Packit 7cfc04
the logical end-of file string is found. The results are unspecified if
Packit 7cfc04
the utility named by
Packit 7cfc04
.IR utility
Packit 7cfc04
attempts to read from its standard input.
Packit 7cfc04
.P
Packit 7cfc04
The generated command line length shall be the sum of the size in bytes
Packit 7cfc04
of the utility name and each argument treated as strings, including a
Packit 7cfc04
null byte terminator for each of these strings. The
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility shall limit the command line length such that when the command
Packit 7cfc04
line is invoked, the combined argument and environment lists (see the
Packit 7cfc04
.IR exec
Packit 7cfc04
family of functions in the System Interfaces volume of POSIX.1\(hy2008) shall not exceed
Packit 7cfc04
{ARG_MAX}\(mi2\|048
Packit 7cfc04
bytes. Within this constraint, if neither the
Packit 7cfc04
.BR \(min
Packit 7cfc04
nor the
Packit 7cfc04
.BR \(mis
Packit 7cfc04
option is specified, the default command line length shall be at least
Packit 7cfc04
{LINE_MAX}.
Packit 7cfc04
.SH OPTIONS
Packit 7cfc04
The
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility shall conform to the Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "Section 12.2" ", " "Utility Syntax Guidelines".
Packit 7cfc04
.P
Packit 7cfc04
The following options shall be supported:
Packit 7cfc04
.IP "\fB\(miE\ \fIeofstr\fR" 10
Packit 7cfc04
Use
Packit 7cfc04
.IR eofstr
Packit 7cfc04
as the logical end-of-file string. If
Packit 7cfc04
.BR \(miE
Packit 7cfc04
is not specified, it is unspecified whether the logical end-of-file
Packit 7cfc04
string is the
Packit 7cfc04
<underscore>
Packit 7cfc04
character (\c
Packit 7cfc04
.BR '_' )
Packit 7cfc04
or the end-of-file string capability is disabled. When
Packit 7cfc04
.IR eofstr
Packit 7cfc04
is the null string, the logical end-of-file string capability shall be
Packit 7cfc04
disabled and
Packit 7cfc04
<underscore>
Packit 7cfc04
characters shall be taken literally.
Packit 7cfc04
.IP "\fB\(miI\ \fIreplstr\fR" 10
Packit 7cfc04
Insert mode:
Packit 7cfc04
.IR utility
Packit 7cfc04
is executed for each logical line from standard input. Arguments in the
Packit 7cfc04
standard input shall be separated only by unescaped
Packit 7cfc04
<newline>
Packit 7cfc04
characters, not by
Packit 7cfc04
<blank>
Packit 7cfc04
characters. Any unquoted unescaped
Packit 7cfc04
<blank>
Packit 7cfc04
characters at the beginning of each line shall be ignored. The resulting
Packit 7cfc04
argument shall be inserted in
Packit 7cfc04
.IR arguments
Packit 7cfc04
in place of each occurrence of
Packit 7cfc04
.IR replstr .
Packit 7cfc04
At least five arguments in
Packit 7cfc04
.IR arguments
Packit 7cfc04
can each contain one or more instances of
Packit 7cfc04
.IR replstr .
Packit 7cfc04
Each of these constructed arguments cannot grow larger than an
Packit 7cfc04
implementation-defined limit greater than or equal to 255 bytes. Option
Packit 7cfc04
.BR \(mix
Packit 7cfc04
shall be forced on.
Packit 7cfc04
.IP "\fB\(miL\ \fInumber\fR" 10
Packit 7cfc04
The
Packit 7cfc04
.IR utility
Packit 7cfc04
shall be executed for each non-empty
Packit 7cfc04
.IR number
Packit 7cfc04
lines of arguments from standard input. The last invocation of
Packit 7cfc04
.IR utility
Packit 7cfc04
shall be with fewer lines of arguments if fewer than
Packit 7cfc04
.IR number
Packit 7cfc04
remain. A line is considered to end with the first
Packit 7cfc04
<newline>
Packit 7cfc04
unless the last character of the line is a
Packit 7cfc04
<blank>;
Packit 7cfc04
a trailing
Packit 7cfc04
<blank>
Packit 7cfc04
signals continuation to the next non-empty line, inclusive.
Packit 7cfc04
.IP "\fB\(min\ \fInumber\fR" 10
Packit 7cfc04
Invoke
Packit 7cfc04
.IR utility
Packit 7cfc04
using as many standard input arguments as possible, up to
Packit 7cfc04
.IR number
Packit 7cfc04
(a positive decimal integer) arguments maximum. Fewer arguments shall
Packit 7cfc04
be used if:
Packit 7cfc04
.RS 10 
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The command line length accumulated exceeds the size specified by the
Packit 7cfc04
.BR \(mis
Packit 7cfc04
option (or
Packit 7cfc04
{LINE_MAX}
Packit 7cfc04
if there is no
Packit 7cfc04
.BR \(mis
Packit 7cfc04
option).
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The last iteration has fewer than
Packit 7cfc04
.IR number ,
Packit 7cfc04
but not zero, operands remaining.
Packit 7cfc04
.RE
Packit 7cfc04
.IP "\fB\(mip\fP" 10
Packit 7cfc04
Prompt mode: the user is asked whether to execute
Packit 7cfc04
.IR utility
Packit 7cfc04
at each invocation. Trace mode (\c
Packit 7cfc04
.BR \(mit )
Packit 7cfc04
is turned on to write the command instance to be executed, followed by
Packit 7cfc04
a prompt to standard error. An affirmative response read from
Packit 7cfc04
.BR /dev/tty
Packit 7cfc04
shall execute the command; otherwise, that particular invocation of
Packit 7cfc04
.IR utility
Packit 7cfc04
shall be skipped.
Packit 7cfc04
.IP "\fB\(mis\ \fIsize\fR" 10
Packit 7cfc04
Invoke
Packit 7cfc04
.IR utility
Packit 7cfc04
using as many standard input arguments as possible yielding a command
Packit 7cfc04
line length less than
Packit 7cfc04
.IR size
Packit 7cfc04
(a positive decimal integer) bytes. Fewer arguments shall be used if:
Packit 7cfc04
.RS 10 
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The total number of arguments exceeds that specified by the
Packit 7cfc04
.BR \(min
Packit 7cfc04
option.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The total number of lines exceeds that specified by the
Packit 7cfc04
.BR \(miL
Packit 7cfc04
option.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
End-of-file is encountered on standard input before
Packit 7cfc04
.IR size
Packit 7cfc04
bytes are accumulated.
Packit 7cfc04
.P
Packit 7cfc04
Values of
Packit 7cfc04
.IR size
Packit 7cfc04
up to at least
Packit 7cfc04
{LINE_MAX}
Packit 7cfc04
bytes shall be supported, provided that the constraints specified in
Packit 7cfc04
the DESCRIPTION are met. It shall not be considered an error if a
Packit 7cfc04
value larger than that supported by the implementation or exceeding the
Packit 7cfc04
constraints specified in the DESCRIPTION is given;
Packit 7cfc04
.IR xargs
Packit 7cfc04
shall use the largest value it supports within the constraints.
Packit 7cfc04
.RE
Packit 7cfc04
.IP "\fB\(mit\fP" 10
Packit 7cfc04
Enable trace mode. Each generated command line shall be written to
Packit 7cfc04
standard error just prior to invocation.
Packit 7cfc04
.IP "\fB\(mix\fP" 10
Packit 7cfc04
Terminate if a constructed command line will not fit in the
Packit 7cfc04
implied or specified size (see the
Packit 7cfc04
.BR \(mis
Packit 7cfc04
option above).
Packit 7cfc04
.SH OPERANDS
Packit 7cfc04
The following operands shall be supported:
Packit 7cfc04
.IP "\fIutility\fR" 10
Packit 7cfc04
The name of the utility to be invoked, found by search path using the
Packit 7cfc04
.IR PATH
Packit 7cfc04
environment variable, described in the Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "Chapter 8" ", " "Environment Variables".
Packit 7cfc04
If
Packit 7cfc04
.IR utility
Packit 7cfc04
is omitted, the default shall be the
Packit 7cfc04
.IR echo
Packit 7cfc04
utility. If the
Packit 7cfc04
.IR utility
Packit 7cfc04
operand names any of the special built-in utilities in
Packit 7cfc04
.IR "Section 2.14" ", " "Special Built-In Utilities",
Packit 7cfc04
the results are undefined.
Packit 7cfc04
.IP "\fIargument\fR" 10
Packit 7cfc04
An initial option or operand for the invocation of
Packit 7cfc04
.IR utility .
Packit 7cfc04
.SH STDIN
Packit 7cfc04
The standard input shall be a text file. The results are unspecified if
Packit 7cfc04
an end-of-file condition is detected immediately following an escaped
Packit 7cfc04
<newline>.
Packit 7cfc04
.SH "INPUT FILES"
Packit 7cfc04
The file
Packit 7cfc04
.BR /dev/tty
Packit 7cfc04
shall be used to read responses required by the
Packit 7cfc04
.BR \(mip
Packit 7cfc04
option.
Packit 7cfc04
.SH "ENVIRONMENT VARIABLES"
Packit 7cfc04
The following environment variables shall affect the execution of
Packit 7cfc04
.IR xargs :
Packit 7cfc04
.IP "\fILANG\fP" 10
Packit 7cfc04
Provide a default value for the internationalization variables that are
Packit 7cfc04
unset or null. (See the Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "Section 8.2" ", " "Internationalization Variables"
Packit 7cfc04
for the precedence of internationalization variables used to determine
Packit 7cfc04
the values of locale categories.)
Packit 7cfc04
.IP "\fILC_ALL\fP" 10
Packit 7cfc04
If set to a non-empty string value, override the values of all the
Packit 7cfc04
other internationalization variables.
Packit 7cfc04
.IP "\fILC_COLLATE\fP" 10
Packit 7cfc04
.br
Packit 7cfc04
Determine the locale for the behavior of ranges, equivalence classes,
Packit 7cfc04
and multi-character collating elements used in the extended regular
Packit 7cfc04
expression defined for the
Packit 7cfc04
.BR yesexpr
Packit 7cfc04
locale keyword in the
Packit 7cfc04
.IR LC_MESSAGES
Packit 7cfc04
category.
Packit 7cfc04
.IP "\fILC_CTYPE\fP" 10
Packit 7cfc04
Determine the locale for the interpretation of sequences of bytes of
Packit 7cfc04
text data as characters (for example, single-byte as opposed to
Packit 7cfc04
multi-byte characters in arguments and input files) and the behavior of
Packit 7cfc04
character classes used in the extended regular expression defined for
Packit 7cfc04
the
Packit 7cfc04
.BR yesexpr
Packit 7cfc04
locale keyword in the
Packit 7cfc04
.IR LC_MESSAGES
Packit 7cfc04
category.
Packit 7cfc04
.IP "\fILC_MESSAGES\fP" 10
Packit 7cfc04
.br
Packit 7cfc04
Determine the locale used to process affirmative responses, and the
Packit 7cfc04
locale used to affect the format and contents of diagnostic messages
Packit 7cfc04
and prompts written to standard error.
Packit 7cfc04
.IP "\fINLSPATH\fP" 10
Packit 7cfc04
Determine the location of message catalogs for the processing of
Packit 7cfc04
.IR LC_MESSAGES .
Packit 7cfc04
.IP "\fIPATH\fP" 10
Packit 7cfc04
Determine the location of
Packit 7cfc04
.IR utility ,
Packit 7cfc04
as described in the Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "Chapter 8" ", " "Environment Variables".
Packit 7cfc04
.SH "ASYNCHRONOUS EVENTS"
Packit 7cfc04
Default.
Packit 7cfc04
.SH STDOUT
Packit 7cfc04
Not used.
Packit 7cfc04
.SH STDERR
Packit 7cfc04
The standard error shall be used for diagnostic messages and the
Packit 7cfc04
.BR \(mit
Packit 7cfc04
and
Packit 7cfc04
.BR \(mip
Packit 7cfc04
options. If the
Packit 7cfc04
.BR \(mit
Packit 7cfc04
option is specified, the
Packit 7cfc04
.IR utility
Packit 7cfc04
and its constructed argument list shall be written to standard error,
Packit 7cfc04
as it will be invoked, prior to invocation. If
Packit 7cfc04
.BR \(mip
Packit 7cfc04
is specified, a prompt of the following format shall be written (in the
Packit 7cfc04
POSIX locale):
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
"?..."
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.P
Packit 7cfc04
at the end of the line of the output from
Packit 7cfc04
.BR \(mit .
Packit 7cfc04
.SH "OUTPUT FILES"
Packit 7cfc04
None.
Packit 7cfc04
.SH "EXTENDED DESCRIPTION"
Packit 7cfc04
None.
Packit 7cfc04
.SH "EXIT STATUS"
Packit 7cfc04
The following exit values shall be returned:
Packit 7cfc04
.IP "\0\0\0\00" 8
Packit 7cfc04
All invocations of
Packit 7cfc04
.IR utility
Packit 7cfc04
returned exit status zero.
Packit 7cfc04
.IP "1\(hy125" 8
Packit 7cfc04
A command line meeting the specified requirements could not be
Packit 7cfc04
assembled, one or more of the invocations of
Packit 7cfc04
.IR utility
Packit 7cfc04
returned a non-zero exit status, or some other error occurred.
Packit 7cfc04
.IP "\0\0126" 8
Packit 7cfc04
The utility specified by
Packit 7cfc04
.IR utility
Packit 7cfc04
was found but could not be invoked.
Packit 7cfc04
.IP "\0\0127" 8
Packit 7cfc04
The utility specified by
Packit 7cfc04
.IR utility
Packit 7cfc04
could not be found.
Packit 7cfc04
.SH "CONSEQUENCES OF ERRORS"
Packit 7cfc04
If a command line meeting the specified requirements cannot be
Packit 7cfc04
assembled, the utility cannot be invoked, an invocation of the utility
Packit 7cfc04
is terminated by a signal, or an invocation of the utility exits with
Packit 7cfc04
exit status 255, the
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility shall write a diagnostic message and exit without processing
Packit 7cfc04
any remaining input.
Packit 7cfc04
.LP
Packit 7cfc04
.IR "The following sections are informative."
Packit 7cfc04
.SH "APPLICATION USAGE"
Packit 7cfc04
The 255 exit status allows a utility being used by
Packit 7cfc04
.IR xargs
Packit 7cfc04
to tell
Packit 7cfc04
.IR xargs
Packit 7cfc04
to terminate if it knows no further invocations using the current data
Packit 7cfc04
stream will succeed. Thus,
Packit 7cfc04
.IR utility
Packit 7cfc04
should explicitly
Packit 7cfc04
.IR exit
Packit 7cfc04
with an appropriate value to avoid accidentally returning with 255.
Packit 7cfc04
.P
Packit 7cfc04
Note that since input is parsed as lines,
Packit 7cfc04
<blank>
Packit 7cfc04
characters separate arguments, and
Packit 7cfc04
<backslash>,
Packit 7cfc04
<apostrophe>,
Packit 7cfc04
and double-quote characters are used for quoting, if
Packit 7cfc04
.IR xargs
Packit 7cfc04
is used to bundle the output of commands like
Packit 7cfc04
.IR find
Packit 7cfc04
.IR dir
Packit 7cfc04
.BR \(miprint
Packit 7cfc04
or
Packit 7cfc04
.IR ls
Packit 7cfc04
into commands to be executed, unexpected results are likely if any
Packit 7cfc04
filenames contain
Packit 7cfc04
<blank>,
Packit 7cfc04
<newline>,
Packit 7cfc04
or quoting characters. This can be solved by using find to call a script
Packit 7cfc04
that converts each file found into a quoted string that is then piped to
Packit 7cfc04
.IR xargs ,
Packit 7cfc04
but in most cases it is preferable just to have
Packit 7cfc04
.IR find
Packit 7cfc04
do the argument aggregation itself by using
Packit 7cfc04
.BR \(miexec
Packit 7cfc04
with a
Packit 7cfc04
.BR '+' 
Packit 7cfc04
terminator instead of
Packit 7cfc04
.BR ';' .
Packit 7cfc04
Note that the quoting rules used by
Packit 7cfc04
.IR xargs
Packit 7cfc04
are not the same as in the shell. They were not made consistent here
Packit 7cfc04
because existing applications depend on the current rules. An easy (but
Packit 7cfc04
inefficient) method that can be used to transform input consisting of
Packit 7cfc04
one argument per line into a quoted form that
Packit 7cfc04
.IR xargs
Packit 7cfc04
interprets correctly is to precede each non-\c
Packit 7cfc04
<newline>
Packit 7cfc04
character with a
Packit 7cfc04
<backslash>.
Packit 7cfc04
More efficient alternatives are shown in Example 2 and Example 5 below.
Packit 7cfc04
.P
Packit 7cfc04
On implementations with a large value for
Packit 7cfc04
{ARG_MAX},
Packit 7cfc04
.IR xargs
Packit 7cfc04
may produce command lines longer than
Packit 7cfc04
{LINE_MAX}.
Packit 7cfc04
For invocation of utilities, this is not a problem. If
Packit 7cfc04
.IR xargs
Packit 7cfc04
is being used to create a text file, users should explicitly set the
Packit 7cfc04
maximum command line length with the
Packit 7cfc04
.BR \(mis
Packit 7cfc04
option.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
.IR command ,
Packit 7cfc04
.IR env ,
Packit 7cfc04
.IR nice ,
Packit 7cfc04
.IR nohup ,
Packit 7cfc04
.IR time ,
Packit 7cfc04
and
Packit 7cfc04
.IR xargs
Packit 7cfc04
utilities have been specified to use exit code 127 if an error occurs
Packit 7cfc04
so that applications can distinguish ``failure to find a utility'' from
Packit 7cfc04
``invoked utility exited with an error indication''. The value 127 was
Packit 7cfc04
chosen because it is not commonly used for other meanings; most
Packit 7cfc04
utilities use small values for ``normal error conditions'' and the
Packit 7cfc04
values above 128 can be confused with termination due to receipt of a
Packit 7cfc04
signal. The value 126 was chosen in a similar manner to indicate that
Packit 7cfc04
the utility could be found, but not invoked. Some scripts produce
Packit 7cfc04
meaningful error messages differentiating the 126 and 127 cases. The
Packit 7cfc04
distinction between exit codes 126 and 127 is based on KornShell
Packit 7cfc04
practice that uses 127 when all attempts to
Packit 7cfc04
.IR exec
Packit 7cfc04
the utility fail with
Packit 7cfc04
.BR [ENOENT] ,
Packit 7cfc04
and uses 126 when any attempt to
Packit 7cfc04
.IR exec
Packit 7cfc04
the utility fails for any other reason.
Packit 7cfc04
.SH EXAMPLES
Packit 7cfc04
.IP " 1." 4
Packit 7cfc04
The following command combines the output of the parenthesized
Packit 7cfc04
commands (minus the
Packit 7cfc04
<apostrophe>
Packit 7cfc04
characters) onto one line, which is then appended to the file log. It
Packit 7cfc04
assumes that the expansion of
Packit 7cfc04
.BR \(dq$0 $*\(dq 
Packit 7cfc04
does not include any
Packit 7cfc04
<apostrophe>
Packit 7cfc04
or
Packit 7cfc04
<newline>
Packit 7cfc04
characters.
Packit 7cfc04
.RS 4 
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
(logname; date; printf "'%s'\en$0 $*") | xargs \(miE "" >>log
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.RE
Packit 7cfc04
.IP " 2." 4
Packit 7cfc04
The following command invokes
Packit 7cfc04
.IR diff
Packit 7cfc04
with successive pairs of arguments originally typed as command line
Packit 7cfc04
arguments. It assumes there are no embedded
Packit 7cfc04
<newline>
Packit 7cfc04
characters in the elements of the original argument list.
Packit 7cfc04
.RS 4 
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
printf "%s\en$@" | sed 's/[^[:alnum:]]/\e\e&/g' |
Packit 7cfc04
    xargs \(miE "" \(min 2 \(mix diff
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.RE
Packit 7cfc04
.IP " 3." 4
Packit 7cfc04
In the following commands, the user is asked which files in the current
Packit 7cfc04
directory (excluding dotfiles) are to be archived. The files are
Packit 7cfc04
archived into
Packit 7cfc04
.BR arch ;
Packit 7cfc04
.IR a ,
Packit 7cfc04
one at a time or
Packit 7cfc04
.IR b ,
Packit 7cfc04
many at a time. The commands assume that no filenames contain
Packit 7cfc04
<blank>,
Packit 7cfc04
<newline>,
Packit 7cfc04
<backslash>,
Packit 7cfc04
<apostrophe>,
Packit 7cfc04
or double-quote characters.
Packit 7cfc04
.RS 4 
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
a. ls | xargs \(miE "" \(mip \(miL 1 ar \(mir arch
Packit 7cfc04
.P
Packit 7cfc04
b. ls | xargs \(miE "" \(mip \(miL 1 | xargs \(miE "" ar \(mir arch
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.RE
Packit 7cfc04
.IP " 4." 4
Packit 7cfc04
The following command invokes
Packit 7cfc04
.IR command1
Packit 7cfc04
one or more times with multiple arguments, stopping if an invocation of
Packit 7cfc04
.IR command1
Packit 7cfc04
has a non-zero exit status.
Packit 7cfc04
.RS 4 
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
xargs \(miE "" sh \(mic 'command1 "$@" || exit 255' sh < xargs_input
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.RE
Packit 7cfc04
.IP " 5." 4
Packit 7cfc04
On XSI-conformant systems, the following command moves all files
Packit 7cfc04
from directory
Packit 7cfc04
.BR $1
Packit 7cfc04
to directory
Packit 7cfc04
.BR $2 ,
Packit 7cfc04
and echoes each move command just before doing it. It assumes no
Packit 7cfc04
filenames contain
Packit 7cfc04
<newline>
Packit 7cfc04
characters and that neither
Packit 7cfc04
.BR $1
Packit 7cfc04
nor
Packit 7cfc04
.BR $2
Packit 7cfc04
contains the sequence
Packit 7cfc04
.BR \(dq{}\(dq .
Packit 7cfc04
.RS 4 
Packit 7cfc04
.sp
Packit 7cfc04
.RS 4
Packit 7cfc04
.nf
Packit 7cfc04
\fB
Packit 7cfc04
ls \(miA "$1" | sed \(mie 's/"/"\e\e""/g' \(mie 's/.*/"&"/' |
Packit 7cfc04
    xargs \(miE "" \(miI {} \(mit mv "$1"/{} "$2"/{}
Packit 7cfc04
.fi \fR
Packit 7cfc04
.P
Packit 7cfc04
.RE
Packit 7cfc04
.RE
Packit 7cfc04
.SH RATIONALE
Packit 7cfc04
The
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility was usually found only in System V-based systems; BSD systems
Packit 7cfc04
included an
Packit 7cfc04
.IR apply
Packit 7cfc04
utility that provided functionality similar to
Packit 7cfc04
.IR xargs
Packit 7cfc04
.BR \(min
Packit 7cfc04
.IR number .
Packit 7cfc04
The SVID lists
Packit 7cfc04
.IR xargs
Packit 7cfc04
as a software development extension. This volume of POSIX.1\(hy2008 does not share the view that
Packit 7cfc04
it is used only for development, and therefore it is not optional.
Packit 7cfc04
.P
Packit 7cfc04
The classic application of the
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility is in conjunction with the
Packit 7cfc04
.IR find
Packit 7cfc04
utility to reduce the number of processes launched by a simplistic use
Packit 7cfc04
of the
Packit 7cfc04
.IR find
Packit 7cfc04
.BR \(miexec
Packit 7cfc04
combination. The
Packit 7cfc04
.IR xargs
Packit 7cfc04
utility is also used to enforce an upper limit on memory required to
Packit 7cfc04
launch a process. With this basis in mind, this volume of POSIX.1\(hy2008 selected only the
Packit 7cfc04
minimal features required.
Packit 7cfc04
.P
Packit 7cfc04
Although the 255 exit status is mostly an accident of historical
Packit 7cfc04
implementations, it allows a utility being used by
Packit 7cfc04
.IR xargs
Packit 7cfc04
to tell
Packit 7cfc04
.IR xargs
Packit 7cfc04
to terminate if it knows no further invocations using the current data
Packit 7cfc04
stream shall succeed. Any non-zero exit status from a utility falls
Packit 7cfc04
into the 1\(hy125 range when
Packit 7cfc04
.IR xargs
Packit 7cfc04
exits. There is no statement of how the various non-zero utility exit
Packit 7cfc04
status codes are accumulated by
Packit 7cfc04
.IR xargs .
Packit 7cfc04
The value could be the addition of all codes, their highest value, the
Packit 7cfc04
last one received, or a single value such as 1. Since no algorithm is
Packit 7cfc04
arguably better than the others, and since many of the standard
Packit 7cfc04
utilities say little more (portably) than ``pass/fail'', no new
Packit 7cfc04
algorithm was invented.
Packit 7cfc04
.P
Packit 7cfc04
Several other
Packit 7cfc04
.IR xargs
Packit 7cfc04
options were removed because simple alternatives already exist within
Packit 7cfc04
\&this volume of POSIX.1\(hy2008. For example, the
Packit 7cfc04
.BR \(mii
Packit 7cfc04
.IR replstr
Packit 7cfc04
option can be just as efficiently performed using a shell
Packit 7cfc04
.BR for
Packit 7cfc04
loop. Since
Packit 7cfc04
.IR xargs
Packit 7cfc04
calls an
Packit 7cfc04
.IR exec
Packit 7cfc04
function with each input line, the
Packit 7cfc04
.BR \(mii
Packit 7cfc04
option does not usually exploit the grouping capabilities of
Packit 7cfc04
.IR xargs .
Packit 7cfc04
.P
Packit 7cfc04
The requirement that
Packit 7cfc04
.IR xargs
Packit 7cfc04
never produces command lines such that invocation of
Packit 7cfc04
.IR utility
Packit 7cfc04
is within 2\|048 bytes of hitting the POSIX
Packit 7cfc04
.IR exec
Packit 7cfc04
{ARG_MAX}
Packit 7cfc04
limitations is intended to guarantee that the invoked utility has room
Packit 7cfc04
to modify its environment variables and command line arguments and
Packit 7cfc04
still be able to invoke another utility. Note that the minimum
Packit 7cfc04
{ARG_MAX}
Packit 7cfc04
allowed by the System Interfaces volume of POSIX.1\(hy2008 is 4\|096 bytes and the minimum
Packit 7cfc04
value allowed by this volume of POSIX.1\(hy2008 is 2\|048 bytes; therefore,
Packit 7cfc04
the 2\|048 bytes difference seems reasonable. Note, however, that
Packit 7cfc04
.IR xargs
Packit 7cfc04
may never be able to invoke a utility if the environment passed in to
Packit 7cfc04
.IR xargs
Packit 7cfc04
comes close to using
Packit 7cfc04
{ARG_MAX}
Packit 7cfc04
bytes.
Packit 7cfc04
.P
Packit 7cfc04
The version of
Packit 7cfc04
.IR xargs
Packit 7cfc04
required by this volume of POSIX.1\(hy2008 is required to wait for the completion of the invoked
Packit 7cfc04
command before invoking another command. This was done because
Packit 7cfc04
historical scripts using
Packit 7cfc04
.IR xargs
Packit 7cfc04
assumed sequential execution. Implementations wanting to provide
Packit 7cfc04
parallel operation of the invoked utilities are encouraged to add an
Packit 7cfc04
option enabling parallel invocation, but should still wait for
Packit 7cfc04
termination of all of the children before
Packit 7cfc04
.IR xargs
Packit 7cfc04
terminates normally.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
.BR \(mie
Packit 7cfc04
option was omitted from the ISO\ POSIX\(hy2:\|1993 standard in the belief that the
Packit 7cfc04
.IR eofstr
Packit 7cfc04
option-argument was recognized only when it was on a line by itself and
Packit 7cfc04
before quote and escape processing were performed, and that the logical
Packit 7cfc04
end-of-file processing was only enabled if a
Packit 7cfc04
.BR \(mie
Packit 7cfc04
option was specified. In that case, a simple
Packit 7cfc04
.IR sed
Packit 7cfc04
script could be used to duplicate the
Packit 7cfc04
.BR \(mie
Packit 7cfc04
functionality. Further investigation revealed that:
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The logical end-of-file string was checked for after quote and escape
Packit 7cfc04
processing, making a
Packit 7cfc04
.IR sed
Packit 7cfc04
script that provided equivalent functionality much more difficult to
Packit 7cfc04
write.
Packit 7cfc04
.IP " *" 4
Packit 7cfc04
The default was to perform logical end-of-file processing with an
Packit 7cfc04
<underscore>
Packit 7cfc04
as the logical end-of-file string.
Packit 7cfc04
.P
Packit 7cfc04
To correct this misunderstanding, the
Packit 7cfc04
.BR \(miE
Packit 7cfc04
.IR eofstr
Packit 7cfc04
option was adopted from the X/Open Portability Guide. Users should
Packit 7cfc04
note that the description of the
Packit 7cfc04
.BR \(miE
Packit 7cfc04
option matches historical documentation of the
Packit 7cfc04
.BR \(mie
Packit 7cfc04
option (which was not adopted because it did not support the Utility
Packit 7cfc04
Syntax Guidelines), by
Packit 7cfc04
saying that if
Packit 7cfc04
.IR eofstr
Packit 7cfc04
is the null string, logical end-of-file processing is disabled.
Packit 7cfc04
Historical implementations of
Packit 7cfc04
.IR xargs
Packit 7cfc04
actually did not disable logical end-of-file processing; they treated a
Packit 7cfc04
null argument found in the input as a logical end-of-file string. (A
Packit 7cfc04
null
Packit 7cfc04
.IR string
Packit 7cfc04
argument could be generated using single or double-quotes (\c
Packit 7cfc04
.BR '\^' 
Packit 7cfc04
or
Packit 7cfc04
.BR \(dq\^\(dq ).
Packit 7cfc04
Since this behavior was not documented historically, it is considered
Packit 7cfc04
to be a bug.
Packit 7cfc04
.P
Packit 7cfc04
The
Packit 7cfc04
.BR \(miI ,
Packit 7cfc04
.BR \(miL ,
Packit 7cfc04
and
Packit 7cfc04
.BR \(min
Packit 7cfc04
options are mutually-exclusive. Some implementations use the last one
Packit 7cfc04
specified if more than one is given on a command line; other
Packit 7cfc04
implementations treat combinations of the options in different ways.
Packit 7cfc04
.SH "FUTURE DIRECTIONS"
Packit 7cfc04
None.
Packit 7cfc04
.SH "SEE ALSO"
Packit 7cfc04
.IR "Chapter 2" ", " "Shell Command Language",
Packit 7cfc04
.IR "\fIdiff\fR\^",
Packit 7cfc04
.IR "\fIecho\fR\^",
Packit 7cfc04
.IR "\fIfind\fR\^"
Packit 7cfc04
.P
Packit 7cfc04
The Base Definitions volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "Chapter 8" ", " "Environment Variables",
Packit 7cfc04
.IR "Section 12.2" ", " "Utility Syntax Guidelines"
Packit 7cfc04
.P
Packit 7cfc04
The System Interfaces volume of POSIX.1\(hy2008,
Packit 7cfc04
.IR "\fIexec\fR\^"
Packit 7cfc04
.SH COPYRIGHT
Packit 7cfc04
Portions of this text are reprinted and reproduced in electronic form
Packit 7cfc04
from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
Packit 7cfc04
-- Portable Operating System Interface (POSIX), The Open Group Base
Packit 7cfc04
Specifications Issue 7, Copyright (C) 2013 by the Institute of
Packit 7cfc04
Electrical and Electronics Engineers, Inc and The Open Group.
Packit 7cfc04
(This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
Packit 7cfc04
event of any discrepancy between this version and the original IEEE and
Packit 7cfc04
The Open Group Standard, the original IEEE and The Open Group Standard
Packit 7cfc04
is the referee document. The original Standard can be obtained online at
Packit 7cfc04
http://www.unix.org/online.html .
Packit 7cfc04
Packit 7cfc04
Any typographical or formatting errors that appear
Packit 7cfc04
in this page are most likely
Packit 7cfc04
to have been introduced during the conversion of the source files to
Packit 7cfc04
man page format. To report such errors, see
Packit 7cfc04
https://www.kernel.org/doc/man-pages/reporting_bugs.html .