1XARGS(1P)                  POSIX Programmer's Manual                 XARGS(1P)
2
3
4

PROLOG

6       This  manual  page is part of the POSIX Programmer's Manual.  The Linux
7       implementation of this interface may differ (consult the  corresponding
8       Linux  manual page for details of Linux behavior), or the interface may
9       not be implemented on Linux.
10
11

NAME

13       xargs — construct argument lists and invoke utility
14

SYNOPSIS

16       xargs [−ptx] [−E eofstr] [−I replstr|−L number|−n number]
17           [−s size] [utility [argument...]]
18

DESCRIPTION

20       The xargs utility shall construct a  command  line  consisting  of  the
21       utility  and  argument operands specified followed by as many arguments
22       read in sequence from standard input as fit in length and  number  con‐
23       straints  specified by the options. The xargs utility shall then invoke
24       the constructed command line and wait for its completion. This sequence
25       shall be repeated until one of the following occurs:
26
27        *  An end-of-file condition is detected on standard input.
28
29        *  An  argument consisting of just the logical end-of-file string (see
30           the −E eofstr option) is found on standard input after double-quote
31           processing,  <apostrophe>  processing,  and <backslash>-escape pro‐
32           cessing (see next paragraph). All arguments up to but not including
33           the  argument  consisting  of  just  the logical end-of-file string
34           shall be used as arguments in constructed command lines.
35
36        *  An invocation of a constructed command line returns an exit  status
37           of 255.
38
39       The  application  shall ensure that arguments in the standard input are
40       separated by unquoted <blank> characters, unescaped <blank> characters,
41       or  <newline>  characters.  A  string  of zero or more non-double-quote
42       ('"') characters and non-<newline> characters can be quoted by  enclos‐
43       ing  them  in  double-quotes. A string of zero or more non-<apostrophe>
44       ('\'') characters and non-<newline> characters can be quoted by enclos‐
45       ing  them  in  <apostrophe>  characters.  Any unquoted character can be
46       escaped by preceding it with a <backslash>.  The utility named by util‐
47       ity  shall  be  executed  one  or  more  times until the end-of-file is
48       reached or the logical end-of file string is  found.  The  results  are
49       unspecified  if  the utility named by utility attempts to read from its
50       standard input.
51
52       The generated command line length shall be the sum of the size in bytes
53       of  the  utility name and each argument treated as strings, including a
54       null byte terminator for each of these strings. The xargs utility shall
55       limit  the  command  line  length  such  that  when the command line is
56       invoked, the combined argument and environment lists (see the exec fam‐
57       ily of functions in the System Interfaces volume of POSIX.1‐2008) shall
58       not exceed {ARG_MAX}−2048 bytes. Within this constraint, if neither the
59       −n  nor  the  −s  option  is specified, the default command line length
60       shall be at least {LINE_MAX}.
61

OPTIONS

63       The xargs utility shall conform  to  the  Base  Definitions  volume  of
64       POSIX.1‐2008, Section 12.2, Utility Syntax Guidelines.
65
66       The following options shall be supported:
67
68       −E eofstr Use  eofstr  as  the logical end-of-file string. If −E is not
69                 specified, it is unspecified whether the logical  end-of-file
70                 string is the <underscore> character ('_') or the end-of-file
71                 string capability  is  disabled.  When  eofstr  is  the  null
72                 string,  the  logical  end-of-file string capability shall be
73                 disabled and <underscore> characters shall  be  taken  liter‐
74                 ally.
75
76       −I replstr
77                 Insert  mode:  utility is executed for each logical line from
78                 standard input. Arguments in the standard input shall be sep‐
79                 arated only by unescaped <newline> characters, not by <blank>
80                 characters. Any unquoted unescaped <blank> characters at  the
81                 beginning  of each line shall be ignored. The resulting argu‐
82                 ment shall be inserted in arguments in place of  each  occur‐
83                 rence  of  replstr.  At least five arguments in arguments can
84                 each contain one or more instances of replstr.  Each of these
85                 constructed  arguments cannot grow larger than an implementa‐
86                 tion-defined limit greater than or equal to 255 bytes. Option
87                 −x shall be forced on.
88
89       −L number The utility shall be executed for each non-empty number lines
90                 of arguments from standard  input.  The  last  invocation  of
91                 utility  shall be with fewer lines of arguments if fewer than
92                 number remain. A line is considered to  end  with  the  first
93                 <newline> unless the last character of the line is a <blank>;
94                 a trailing <blank> signals continuation to the next non-empty
95                 line, inclusive.
96
97       −n number Invoke utility using as many standard input arguments as pos‐
98                 sible, up to number (a positive  decimal  integer)  arguments
99                 maximum. Fewer arguments shall be used if:
100
101                  *  The  command  line  length  accumulated  exceeds the size
102                     specified by the −s option (or {LINE_MAX} if there is  no
103                     −s option).
104
105                  *  The  last  iteration has fewer than number, but not zero,
106                     operands remaining.
107
108       −p        Prompt mode: the user is asked whether to execute utility  at
109                 each  invocation.  Trace  mode (−t) is turned on to write the
110                 command instance to be executed,  followed  by  a  prompt  to
111                 standard  error.  An  affirmative response read from /dev/tty
112                 shall execute the command; otherwise, that particular invoca‐
113                 tion of utility shall be skipped.
114
115       −s size   Invoke utility using as many standard input arguments as pos‐
116                 sible yielding a command line length less than size (a  posi‐
117                 tive  decimal  integer)  bytes. Fewer arguments shall be used
118                 if:
119
120                  *  The total number of arguments exceeds that  specified  by
121                     the −n option.
122
123                  *  The  total  number of lines exceeds that specified by the
124                     −L option.
125
126                  *  End-of-file is encountered on standard input before  size
127                     bytes are accumulated.
128
129                 Values  of size up to at least {LINE_MAX} bytes shall be sup‐
130                 ported,  provided  that  the  constraints  specified  in  the
131                 DESCRIPTION are met. It shall not be considered an error if a
132                 value larger than that supported  by  the  implementation  or
133                 exceeding  the  constraints  specified  in the DESCRIPTION is
134                 given; xargs shall use the largest value it  supports  within
135                 the constraints.
136
137       −t        Enable trace mode. Each generated command line shall be writ‐
138                 ten to standard error just prior to invocation.
139
140       −x        Terminate if a constructed command line will not fit  in  the
141                 implied or specified size (see the −s option above).
142

OPERANDS

144       The following operands shall be supported:
145
146       utility   The  name  of the utility to be invoked, found by search path
147                 using the PATH environment variable, described  in  the  Base
148                 Definitions  volume  of  POSIX.1‐2008, Chapter 8, Environment
149                 Variables.  If utility is omitted, the default shall  be  the
150                 echo utility. If the utility operand names any of the special
151                 built-in utilities in Section 2.14, Special  Built-In  Utili‐
152                 ties, the results are undefined.
153
154       argument  An initial option or operand for the invocation of utility.
155

STDIN

157       The standard input shall be a text file. The results are unspecified if
158       an end-of-file condition is detected immediately following  an  escaped
159       <newline>.
160

INPUT FILES

162       The  file  /dev/tty  shall be used to read responses required by the −p
163       option.
164

ENVIRONMENT VARIABLES

166       The following environment  variables  shall  affect  the  execution  of
167       xargs:
168
169       LANG      Provide  a  default  value for the internationalization vari‐
170                 ables that are unset or null. (See the Base Definitions  vol‐
171                 ume  of POSIX.1‐2008, Section 8.2, Internationalization Vari‐
172                 ables for the precedence  of  internationalization  variables
173                 used to determine the values of locale categories.)
174
175       LC_ALL    If  set  to  a non-empty string value, override the values of
176                 all the other internationalization variables.
177
178       LC_COLLATE
179                 Determine the locale for the behavior of ranges,  equivalence
180                 classes,  and  multi-character collating elements used in the
181                 extended regular expression defined for  the  yesexpr  locale
182                 keyword in the LC_MESSAGES category.
183
184       LC_CTYPE  Determine  the  locale for the interpretation of sequences of
185                 bytes of text data as characters (for example, single-byte as
186                 opposed  to  multi-byte  characters  in  arguments  and input
187                 files) and the behavior of  character  classes  used  in  the
188                 extended  regular  expression  defined for the yesexpr locale
189                 keyword in the LC_MESSAGES category.
190
191       LC_MESSAGES
192                 Determine the locale used to process  affirmative  responses,
193                 and  the  locale  used  to  affect the format and contents of
194                 diagnostic messages and prompts written to standard error.
195
196       NLSPATH   Determine the location of message catalogs for the processing
197                 of LC_MESSAGES.
198
199       PATH      Determine  the  location of utility, as described in the Base
200                 Definitions volume of POSIX.1‐2008,  Chapter  8,  Environment
201                 Variables.
202

ASYNCHRONOUS EVENTS

204       Default.
205

STDOUT

207       Not used.
208

STDERR

210       The standard error shall be used for diagnostic messages and the −t and
211       −p options. If the −t option is specified, the  utility  and  its  con‐
212       structed  argument  list shall be written to standard error, as it will
213       be invoked, prior to invocation. If −p is specified, a  prompt  of  the
214       following format shall be written (in the POSIX locale):
215
216           "?..."
217
218       at the end of the line of the output from −t.
219

OUTPUT FILES

221       None.
222

EXTENDED DESCRIPTION

224       None.
225

EXIT STATUS

227       The following exit values shall be returned:
228
229           0   All invocations of utility returned exit status zero.
230
231       1‐125   A  command line meeting the specified requirements could not be
232               assembled, one or more of the invocations of utility returned a
233               non-zero exit status, or some other error occurred.
234
235         126   The  utility  specified  by  utility was found but could not be
236               invoked.
237
238         127   The utility specified by utility could not be found.
239

CONSEQUENCES OF ERRORS

241       If a command line meeting the specified requirements cannot  be  assem‐
242       bled,  the  utility  cannot be invoked, an invocation of the utility is
243       terminated by a signal, or an invocation of the utility exits with exit
244       status 255, the xargs utility shall write a diagnostic message and exit
245       without processing any remaining input.
246
247       The following sections are informative.
248

APPLICATION USAGE

250       The 255 exit status allows a utility being used by xargs to tell  xargs
251       to  terminate if it knows no further invocations using the current data
252       stream will succeed. Thus,  utility  should  explicitly  exit  with  an
253       appropriate value to avoid accidentally returning with 255.
254
255       Note  that  since input is parsed as lines, <blank> characters separate
256       arguments, and <backslash>, <apostrophe>, and  double-quote  characters
257       are used for quoting, if xargs is used to bundle the output of commands
258       like find dir −print or ls into commands  to  be  executed,  unexpected
259       results  are  likely  if  any  filenames contain <blank>, <newline>, or
260       quoting characters. This can be solved by using find to call  a  script
261       that  converts  each file found into a quoted string that is then piped
262       to xargs, but in most cases it is preferable just to have find  do  the
263       argument  aggregation  itself  by  using  −exec  with  a '+' terminator
264       instead of ';'.  Note that the quoting rules used by xargs are not  the
265       same as in the shell. They were not made consistent here because exist‐
266       ing applications depend on the current rules. An easy (but inefficient)
267       method  that  can be used to transform input consisting of one argument
268       per line into a quoted form that xargs interprets correctly is to  pre‐
269       cede  each  non-<newline> character with a <backslash>.  More efficient
270       alternatives are shown in Example 2 and Example 5 below.
271
272       On implementations with a large value for {ARG_MAX}, xargs may  produce
273       command  lines  longer  than  {LINE_MAX}.  For invocation of utilities,
274       this is not a problem. If xargs is being used to create  a  text  file,
275       users should explicitly set the maximum command line length with the −s
276       option.
277
278       The command, env, nice, nohup, time,  and  xargs  utilities  have  been
279       specified  to use exit code 127 if an error occurs so that applications
280       can distinguish ``failure to find a utility''  from  ``invoked  utility
281       exited  with an error indication''. The value 127 was chosen because it
282       is not commonly used for other meanings; most utilities use small  val‐
283       ues  for  ``normal  error  conditions'' and the values above 128 can be
284       confused with termination due to receipt of a signal. The value 126 was
285       chosen in a similar manner to indicate that the utility could be found,
286       but not invoked. Some scripts produce meaningful error messages differ‐
287       entiating the 126 and 127 cases. The distinction between exit codes 126
288       and 127 is based on KornShell practice that uses 127 when all  attempts
289       to  exec  the utility fail with [ENOENT], and uses 126 when any attempt
290       to exec the utility fails for any other reason.
291

EXAMPLES

293        1. The following command combines the output of the parenthesized com‐
294           mands  (minus  the <apostrophe> characters) onto one line, which is
295           then appended to the file log. It assumes  that  the  expansion  of
296           "$0$*" does not include any <apostrophe> or <newline> characters.
297
298               (logname; date; printf "'%s'\n$0 $*") | xargs −E "" >>log
299
300        2. The  following  command invokes diff with successive pairs of argu‐
301           ments originally typed as command line arguments. It assumes  there
302           are  no embedded <newline> characters in the elements of the origi‐
303           nal argument list.
304
305               printf "%s\n$@" | sed 's/[^[:alnum:]]/\\&/g' |
306                   xargs −E "" −n 2 −x diff
307
308        3. In the following commands, the user is asked  which  files  in  the
309           current  directory  (excluding  dotfiles)  are  to be archived. The
310           files are archived into arch; a, one at a time  or  b,  many  at  a
311           time.  The commands assume that no filenames contain <blank>, <new‐
312           line>, <backslash>, <apostrophe>, or double-quote characters.
313
314               a. ls | xargs −E "" −p −L 1 ar −r arch
315
316               b. ls | xargs −E "" −p −L 1 | xargs −E "" ar −r arch
317
318        4. The following command invokes command1 one or more times with  mul‐
319           tiple  arguments,  stopping if an invocation of command1 has a non-
320           zero exit status.
321
322               xargs −E "" sh −c 'command1 "$@" || exit 255' sh < xargs_input
323
324        5. On XSI-conformant systems, the following command  moves  all  files
325           from  directory  $1  to  directory $2, and echoes each move command
326           just before doing it. It assumes  no  filenames  contain  <newline>
327           characters and that neither $1 nor $2 contains the sequence "{}".
328
329               ls −A "$1" | sed −e 's/"/"\\""/g' −e 's/.*/"&"/' |
330                   xargs −E "" −I {} −t mv "$1"/{} "$2"/{}
331

RATIONALE

333       The xargs utility was usually found only in System V-based systems; BSD
334       systems included an apply utility that provided  functionality  similar
335       to  xargs  −n  number.   The SVID lists xargs as a software development
336       extension. This volume of POSIX.1‐2008 does not share the view that  it
337       is used only for development, and therefore it is not optional.
338
339       The classic application of the xargs utility is in conjunction with the
340       find utility to reduce the number of processes launched by a simplistic
341       use  of  the  find −exec combination. The xargs utility is also used to
342       enforce an upper limit on memory required to  launch  a  process.  With
343       this basis in mind, this volume of POSIX.1‐2008 selected only the mini‐
344       mal features required.
345
346       Although the 255 exit status is mostly an accident of historical imple‐
347       mentations,  it  allows  a utility being used by xargs to tell xargs to
348       terminate if it knows no further invocations  using  the  current  data
349       stream  shall  succeed.  Any  non-zero exit status from a utility falls
350       into the 1‐125 range when xargs exits. There is no statement of how the
351       various  non-zero  utility  exit status codes are accumulated by xargs.
352       The value could be the addition of all codes, their highest value,  the
353       last  one  received, or a single value such as 1. Since no algorithm is
354       arguably better than the others, and since many of the standard  utili‐
355       ties  say  little  more (portably) than ``pass/fail'', no new algorithm
356       was invented.
357
358       Several other xargs options were removed  because  simple  alternatives
359       already  exist  within this volume of POSIX.1‐2008. For example, the −i
360       replstr option can be just as efficiently performed using a  shell  for
361       loop.  Since  xargs calls an exec function with each input line, the −i
362       option does not usually exploit the grouping capabilities of xargs.
363
364       The requirement that xargs never produces command lines such that invo‐
365       cation  of  utility  is  within  2048  bytes  of hitting the POSIX exec
366       {ARG_MAX} limitations is intended to guarantee that the invoked utility
367       has room to modify its environment variables and command line arguments
368       and still be able to invoke another  utility.  Note  that  the  minimum
369       {ARG_MAX}  allowed  by  the System Interfaces volume of POSIX.1‐2008 is
370       4096 bytes and the minimum value allowed by this volume of POSIX.1‐2008
371       is  2048  bytes; therefore, the 2048 bytes difference seems reasonable.
372       Note, however, that xargs may never be able to invoke a utility if  the
373       environment passed in to xargs comes close to using {ARG_MAX} bytes.
374
375       The  version  of  xargs  required  by  this  volume  of POSIX.1‐2008 is
376       required to wait for the  completion  of  the  invoked  command  before
377       invoking  another  command.  This  was  done because historical scripts
378       using xargs assumed sequential execution.  Implementations  wanting  to
379       provide  parallel  operation of the invoked utilities are encouraged to
380       add an option enabling parallel invocation, but should still  wait  for
381       termination of all of the children before xargs terminates normally.
382
383       The  −e  option  was  omitted from the ISO POSIX‐2:1993 standard in the
384       belief that the eofstr option-argument was recognized only when it  was
385       on  a  line  by itself and before quote and escape processing were per‐
386       formed, and that the logical end-of-file processing was only enabled if
387       a  −e  option was specified. In that case, a simple sed script could be
388       used to duplicate the −e functionality. Further investigation  revealed
389       that:
390
391        *  The  logical  end-of-file  string  was  checked for after quote and
392           escape processing, making a sed  script  that  provided  equivalent
393           functionality much more difficult to write.
394
395        *  The  default  was to perform logical end-of-file processing with an
396           <underscore> as the logical end-of-file string.
397
398       To correct this misunderstanding, the −E eofstr option was adopted from
399       the X/Open Portability Guide. Users should note that the description of
400       the −E option matches historical documentation of the −e option  (which
401       was  not  adopted  because it did not support the Utility Syntax Guide‐
402       lines), by saying that if eofstr is the null  string,  logical  end-of-
403       file processing is disabled.  Historical implementations of xargs actu‐
404       ally did not disable logical end-of-file  processing;  they  treated  a
405       null  argument  found  in the input as a logical end-of-file string. (A
406       null string argument could be generated using single  or  double-quotes
407       ('' or "").  Since this behavior was not documented historically, it is
408       considered to be a bug.
409
410       The −I, −L, and −n options are mutually-exclusive. Some implementations
411       use the last one specified if more than one is given on a command line;
412       other implementations treat combinations of the  options  in  different
413       ways.
414

FUTURE DIRECTIONS

416       None.
417

SEE ALSO

419       Chapter 2, Shell Command Language, diff, echo, find
420
421       The  Base  Definitions  volume  of POSIX.1‐2008, Chapter 8, Environment
422       Variables, Section 12.2, Utility Syntax Guidelines
423
424       The System Interfaces volume of POSIX.1‐2008, exec
425
427       Portions of this text are reprinted and reproduced in  electronic  form
428       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
429       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
430       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
431       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
432       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
433       event of any discrepancy between this version and the original IEEE and
434       The  Open Group Standard, the original IEEE and The Open Group Standard
435       is the referee document. The original Standard can be  obtained  online
436       at http://www.unix.org/online.html .
437
438       Any  typographical  or  formatting  errors that appear in this page are
439       most likely to have been introduced during the conversion of the source
440       files  to  man page format. To report such errors, see https://www.ker
441       nel.org/doc/man-pages/reporting_bugs.html .
442
443
444
445IEEE/The Open Group                  2013                            XARGS(1P)
Impressum