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

NAME

12       xargs - construct argument lists and invoke utility
13

SYNOPSIS

15       xargs [-t][-p]][-E eofstr][-I replstr][-L number][-n number [-x]]
16               [-s size][utility [argument...]]
17

DESCRIPTION

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

OPTIONS

58       The xargs utility shall conform  to  the  Base  Definitions  volume  of
59       IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
60
61       The following options shall be supported:
62
63       -E  eofstr
64              Use eofstr as the logical end-of-file string. If -E is not spec‐
65              ified, it is unspecified whether the logical end-of-file  string
66              is  the  underscore  character ( '_' ) or the end-of-file string
67              capability is disabled. When eofstr is the null string, the log‐
68              ical  end-of-file string capability shall be disabled and under‐
69              score characters shall be taken literally.
70
71       -I  replstr
72              Insert mode: utility is executed for  each  line  from  standard
73              input, taking the entire line as a single argument, inserting it
74              in arguments for each occurrence of replstr. A maximum  of  five
75              arguments in arguments can each contain one or more instances of
76              replstr. Any <blank>s at the beginning of  each  line  shall  be
77              ignored.  Constructed  arguments  cannot  grow  larger  than 255
78              bytes. Option -x shall be forced on.
79
80       -L  number
81              The utility shall be executed for each non-empty number lines of
82              arguments  from  standard  input. The last invocation of utility
83              shall be with fewer lines of  arguments  if  fewer  than  number
84              remain.  A  line  is  considered to end with the first <newline>
85              unless the last character of the line is a <blank>;  a  trailing
86              <blank>  signals continuation to the next non-empty line, inclu‐
87              sive. The -L and -n options are mutually-exclusive; the last one
88              specified shall take effect.
89
90       -n  number
91              Invoke  utility using as many standard input arguments as possi‐
92              ble, up to number (a positive decimal integer)  arguments  maxi‐
93              mum. Fewer arguments shall be used if:
94
95               * The  command  line length accumulated exceeds the size speci‐
96                 fied by the -s option  (or  {LINE_MAX}  if  there  is  no  -s
97                 option).
98
99               * The last iteration has fewer than number, but not zero, oper‐
100                 ands remaining.
101
102       -p     Prompt mode: the user is asked whether  to  execute  utility  at
103              each invocation. Trace mode ( -t) is turned on to write the com‐
104              mand instance to be executed, followed by a prompt  to  standard
105              error.  An affirmative response read from /dev/tty shall execute
106              the command; otherwise, that particular  invocation  of  utility
107              shall be skipped.
108
109       -s  size
110              Invoke  utility using as many standard input arguments as possi‐
111              ble yielding a command line length less than  size  (a  positive
112              decimal integer) bytes. Fewer arguments shall be used if:
113
114               * The  total  number of arguments exceeds that specified by the
115                 -n option.
116
117               * The total number of lines exceeds that specified  by  the  -L
118                 option.
119
120               * End-of-file  is  encountered  on  standard  input before size
121                 bytes are accumulated.
122
123       Values of size up to at least {LINE_MAX} bytes shall be supported, pro‐
124       vided  that  the  constraints  specified in the DESCRIPTION are met. It
125       shall not be considered an error if a value larger than that  supported
126       by  the  implementation  or  exceeding the constraints specified in the
127       DESCRIPTION is given; xargs shall use the  largest  value  it  supports
128       within the constraints.
129
130       -t     Enable  trace mode. Each generated command line shall be written
131              to standard error just prior to invocation.
132
133       -x     Terminate if a command line containing number arguments (see the
134              -n  option  above)   or  number  lines (see the -L option above)
135              will not fit in the implied or specified size (see the -s option
136              above).
137
138

OPERANDS

140       The following operands shall be supported:
141
142       utility
143              The  name  of  the  utility  to be invoked, found by search path
144              using the PATH environment variable, described in the Base Defi‐
145              nitions  volume  of IEEE Std 1003.1-2001, Chapter 8, Environment
146              Variables.  If utility is omitted, the default shall be the echo
147              utility.  If the utility operand names any of the special built-
148              in utilities in Special  Built-In  Utilities,  the  results  are
149              undefined.
150
151       argument
152              An initial option or operand for the invocation of utility.
153
154

STDIN

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

INPUT FILES

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

ENVIRONMENT VARIABLES

165       The following environment  variables  shall  affect  the  execution  of
166       xargs:
167
168       LANG   Provide  a  default value for the internationalization variables
169              that are unset or null. (See  the  Base  Definitions  volume  of
170              IEEE Std 1003.1-2001,  Section  8.2,  Internationalization Vari‐
171              ables for the precedence of internationalization variables  used
172              to determine the values of locale categories.)
173
174       LC_ALL If  set  to a non-empty string value, override the values of all
175              the other internationalization variables.
176
177       LC_COLLATE
178
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  key‐
182              word in the LC_MESSAGES category.
183
184       LC_CTYPE
185              Determine  the  locale  for  the  interpretation of sequences of
186              bytes of text data as characters (for  example,  single-byte  as
187              opposed  to  multi-byte characters in arguments and input files)
188              and the behavior of character classes used in the extended regu‐
189              lar  expression  defined  for  the yesexpr locale keyword in the
190              LC_MESSAGES category.
191
192       LC_MESSAGES
193              Determine the locale for the processing of affirmative responses
194              and  that  should  be  used to affect the format and contents of
195              diagnostic messages written to standard error.
196
197       NLSPATH
198              Determine the location of message catalogs for the processing of
199              LC_MESSAGES .
200
201       PATH   Determine the location of utility, as described in the Base Def‐
202              initions volume of IEEE Std 1003.1-2001, Chapter 8,  Environment
203              Variables.
204
205

ASYNCHRONOUS EVENTS

207       Default.
208

STDOUT

210       Not used.
211

STDERR

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

OUTPUT FILES

225       None.
226

EXTENDED DESCRIPTION

228       None.
229

EXIT STATUS

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

CONSEQUENCES OF ERRORS

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

APPLICATION USAGE

255       The 255 exit status allows a utility being used by xargs to tell  xargs
256       to  terminate if it knows no further invocations using the current data
257       stream will succeed. Thus,  utility  should  explicitly  exit  with  an
258       appropriate value to avoid accidentally returning with 255.
259
260       Note  that  input  is  parsed as lines; <blank>s separate arguments. If
261       xargs is used to bundle output of commands like find dir -print  or  ls
262       into  commands  to  be  executed,  unexpected results are likely if any
263       filenames contain any <blank>s or <newline>s.  This  can  be  fixed  by
264       using find to call a script that converts each file found into a quoted
265       string that is then piped to xargs. Note that the quoting rules used by
266       xargs  are  not the same as in the shell. They were not made consistent
267       here because existing applications depend on the current rules and  the
268       shell  syntax is not fully compatible with it. An easy rule that can be
269       used to transform any string into a quoted form that  xargs  interprets
270       correctly is to precede each character in the string with a backslash.
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, this
274       is  not  a problem. If xargs is being used to create a text file, users
275       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 con‐
284       fused  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 parenthesised com‐
294           mands onto one line, which is then written to the end-of-file log:
295
296
297           (logname; date; printf "%s\n" "$0 $*") | xargs >>log
298
299        2. The following command invokes diff with successive pairs  of  argu‐
300           ments  originally  typed  as command line arguments (assuming there
301           are no embedded <blank>s in the elements of the  original  argument
302           list):
303
304
305           printf "%s\n" "$*" | xargs -n 2 -x diff
306
307        3. In  the  following  commands,  the user is asked which files in the
308           current directory are to be archived. The files are  archived  into
309           arch; a, one at a time, or b, many at a time.
310
311
312           a. ls | xargs -p -L 1 ar -r arch
313
314
315           b. ls | xargs -p -L 1 | xargs ar -r arch
316
317        4. The  following  executes  with successive pairs of arguments origi‐
318           nally typed as command line arguments:
319
320
321           echo $* | xargs -n 2 diff
322
323        5. On XSI-conformant systems,  the  following  moves  all  files  from
324           directory  $1  to  directory  $2, and echoes each move command just
325           before doing it:
326
327
328           ls $1 | xargs -I {} -t mv $1/{} $2/{}
329

RATIONALE

331       The xargs utility was usually found only in System V-based systems; BSD
332       systems  included  an apply utility that provided functionality similar
333       to xargs -n number.  The SVID lists xargs  as  a  software  development
334       extension.  This volume of IEEE Std 1003.1-2001 does not share the view
335       that it is used only for development, and therefore it is not optional.
336
337       The classic application of the xargs utility is in conjunction with the
338       find utility to reduce the number of processes launched by a simplistic
339       use of the find -exec combination. The xargs utility is  also  used  to
340       enforce  an  upper  limit on memory required to launch a process.  With
341       this basis in mind, this volume of IEEE Std 1003.1-2001  selected  only
342       the minimal features required.
343
344       Although the 255 exit status is mostly an accident of historical imple‐
345       mentations, it allows a utility being used by xargs to  tell  xargs  to
346       terminate  if  it  knows  no further invocations using the current data
347       stream shall succeed. Any non-zero exit status  from  a  utility  falls
348       into the 1-125 range when xargs exits. There is no statement of how the
349       various non-zero utility exit status codes are  accumulated  by  xargs.
350       The  value could be the addition of all codes, their highest value, the
351       last one received, or a single value such as 1. Since no  algorithm  is
352       arguably  better than the others, and since many of the standard utili‐
353       ties say little more (portably) than "pass/fail", no new algorithm  was
354       invented.
355
356       Several  other xargs options were withdrawn because simple alternatives
357       already exist within this volume of IEEE Std 1003.1-2001. For  example,
358       the  -i  replstr  option  can  be just as efficiently performed using a
359       shell for loop. Since xargs calls an  exec  function  with  each  input
360       line,  the -i option does not usually exploit the grouping capabilities
361       of xargs.
362
363       The requirement that xargs never produces command lines such that invo‐
364       cation  of  utility  is  within  2048  bytes  of hitting the POSIX exec
365       {ARG_MAX} limitations is intended to guarantee that the invoked utility
366       has room to modify its environment variables and command line arguments
367       and still be able to invoke another  utility.  Note  that  the  minimum
368       {ARG_MAX}    allowed    by    the    System    Interfaces   volume   of
369       IEEE Std 1003.1-2001 is 4096 bytes and the  minimum  value  allowed  by
370       this  volume of IEEE Std 1003.1-2001 is 2048 bytes; therefore, the 2048
371       bytes difference seems reasonable. Note, however, that xargs may  never
372       be able to invoke a utility if the environment passed in to xargs comes
373       close to using {ARG_MAX} bytes.
374
375       The version of xargs required by this volume of IEEE Std 1003.1-2001 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

FUTURE DIRECTIONS

411       None.
412

SEE ALSO

414       Shell  Command  Language,  echo,  find, the System Interfaces volume of
415       IEEE Std 1003.1-2001, exec
416
418       Portions of this text are reprinted and reproduced in  electronic  form
419       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
420       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
421       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
422       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
423       event of any discrepancy between this version and the original IEEE and
424       The Open Group Standard, the original IEEE and The Open Group  Standard
425       is  the  referee document. The original Standard can be obtained online
426       at http://www.opengroup.org/unix/online.html .
427
428
429
430IEEE/The Open Group                  2003                            XARGS(1P)
Impressum