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

OPTIONS

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

OPERANDS

143       The following operands shall be supported:
144
145       utility   The  name  of the utility to be invoked, found by search path
146                 using the PATH environment variable, described  in  the  Base
147                 Definitions  volume  of  POSIX.1‐2017, Chapter 8, Environment
148                 Variables.  If utility is omitted, the default shall  be  the
149                 echo utility. If the utility operand names any of the special
150                 built-in utilities in Section 2.14, Special  Built-In  Utili‐
151                 ties, the results are undefined.
152
153       argument  An initial option or operand for the invocation of utility.
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 vari‐
169                 ables that are unset or null. (See the Base Definitions  vol‐
170                 ume  of POSIX.1‐2017, Section 8.2, Internationalization Vari‐
171                 ables for the precedence  of  internationalization  variables
172                 used to determine the values of locale categories.)
173
174       LC_ALL    If  set  to  a non-empty string value, override the values of
175                 all the other internationalization variables.
176
177       LC_COLLATE
178                 Determine the locale for the behavior of ranges,  equivalence
179                 classes,  and  multi-character collating elements used in the
180                 extended regular expression defined for  the  yesexpr  locale
181                 keyword in the LC_MESSAGES category.
182
183       LC_CTYPE  Determine  the  locale for the interpretation of sequences of
184                 bytes of text data as characters (for example, single-byte as
185                 opposed  to  multi-byte  characters  in  arguments  and input
186                 files) and the behavior of  character  classes  used  in  the
187                 extended  regular  expression  defined for the yesexpr locale
188                 keyword in the LC_MESSAGES category.
189
190       LC_MESSAGES
191                 Determine the locale used to process  affirmative  responses,
192                 and  the  locale  used  to  affect the format and contents of
193                 diagnostic messages and prompts written to standard error.
194
195       NLSPATH   Determine the location of message catalogs for the processing
196                 of LC_MESSAGES.
197
198       PATH      Determine  the  location of utility, as described in the Base
199                 Definitions volume of POSIX.1‐2017,  Chapter  8,  Environment
200                 Variables.
201

ASYNCHRONOUS EVENTS

203       Default.
204

STDOUT

206       Not used.
207

STDERR

209       The standard error shall be used for diagnostic messages and the -t and
210       -p options. If the -t option is specified, the  utility  and  its  con‐
211       structed  argument  list shall be written to standard error, as it will
212       be invoked, prior to invocation. If -p is specified, a  prompt  of  the
213       following format shall be written (in the POSIX locale):
214
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
299               (logname; date; printf "'%s'\n$0 $*") | xargs -E "" >>log
300
301        2. The  following  command invokes diff with successive pairs of argu‐
302           ments originally typed as command line arguments. It assumes  there
303           are  no embedded <newline> characters in the elements of the origi‐
304           nal argument list.
305
306
307               printf "%s\n$@" | sed 's/[^[:alnum:]]/\\&/g' |
308                   xargs -E "" -n 2 -x diff
309
310        3. In the following commands, the user is asked  which  files  in  the
311           current  directory  (excluding  dotfiles)  are  to be archived. The
312           files are archived into arch; a, one at a time  or  b,  many  at  a
313           time.  The commands assume that no filenames contain <blank>, <new‐
314           line>, <backslash>, <apostrophe>, or double-quote characters.
315
316
317               a. ls | xargs -E "" -p -L 1 ar -r arch
318
319               b. ls | xargs -E "" -p -L 1 | xargs -E "" ar -r arch
320
321        4. The following command invokes command1 one or more times with  mul‐
322           tiple  arguments,  stopping if an invocation of command1 has a non-
323           zero exit status.
324
325
326               xargs -E "" sh -c 'command1 "$@" || exit 255' sh < xargs_input
327
328        5. On XSI-conformant systems, the following command  moves  all  files
329           from  directory  $1  to  directory $2, and echoes each move command
330           just before doing it. It assumes  no  filenames  contain  <newline>
331           characters and that neither $1 nor $2 contains the sequence "{}".
332
333
334               ls -A "$1" | sed -e 's/"/"\\""/g' -e 's/.*/"&"/' |
335                   xargs -E "" -I {} -t mv "$1"/{} "$2"/{}
336

RATIONALE

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

FUTURE DIRECTIONS

421       None.
422

SEE ALSO

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