1XARGS(P) POSIX Programmer's Manual XARGS(P)
2
3
4
6 xargs - construct argument lists and invoke utility
7
9 xargs [-t][-p]][-E eofstr][-I replstr][-L number][-n number [-x]]
10 [-s size][utility [argument...]]
11
13 The xargs utility shall construct a command line consisting of the
14 utility and argument operands specified followed by as many arguments
15 read in sequence from standard input as fit in length and number con‐
16 straints specified by the options. The xargs utility shall then invoke
17 the constructed command line and wait for its completion. This sequence
18 shall be repeated until one of the following occurs:
19
20 * An end-of-file condition is detected on standard input.
21
22 * The logical end-of-file string (see the -E eofstr option) is found
23 on standard input after double-quote processing, apostrophe process‐
24 ing, and backslash escape processing (see next paragraph).
25
26 * An invocation of a constructed command line returns an exit status
27 of 255.
28
29 The application shall ensure that arguments in the standard input are
30 separated by unquoted <blank>s, unescaped <blank>s, or <newline>s. A
31 string of zero or more non-double-quote ( ' )' characters and non-
32 <newline>s can be quoted by enclosing them in double-quotes. A string
33 of zero or more non-apostrophe ( '" ) characters and non- <newline>s
34 can be quoted by enclosing them in apostrophes. Any unquoted character
35 can be escaped by preceding it with a backslash. The utility named by
36 utility shall be executed one or more times until the end-of-file is
37 reached or the logical end-of file string is found. The results are
38 unspecified if the utility named by utility attempts to read from its
39 standard input.
40
41 The generated command line length shall be the sum of the size in bytes
42 of the utility name and each argument treated as strings, including a
43 null byte terminator for each of these strings. The xargs utility
44 shall limit the command line length such that when the command line is
45 invoked, the combined argument and environment lists (see the exec fam‐
46 ily of functions in the System Interfaces volume of
47 IEEE Std 1003.1-2001) shall not exceed {ARG_MAX}-2048 bytes. Within
48 this constraint, if neither the -n nor the -s option is specified, the
49 default command line length shall be at least {LINE_MAX}.
50
52 The xargs utility shall conform to the Base Definitions volume of
53 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines.
54
55 The following options shall be supported:
56
57 -E eofstr
58 Use eofstr as the logical end-of-file string. If -E is not spec‐
59 ified, it is unspecified whether the logical end-of-file string
60 is the underscore character ( '_' ) or the end-of-file string
61 capability is disabled. When eofstr is the null string, the log‐
62 ical end-of-file string capability shall be disabled and under‐
63 score characters shall be taken literally.
64
65 -I replstr
66 Insert mode: utility is executed for each line from standard
67 input, taking the entire line as a single argument, inserting it
68 in arguments for each occurrence of replstr. A maximum of five
69 arguments in arguments can each contain one or more instances of
70 replstr. Any <blank>s at the beginning of each line shall be
71 ignored. Constructed arguments cannot grow larger than 255
72 bytes. Option -x shall be forced on.
73
74 -L number
75 The utility shall be executed for each non-empty number lines of
76 arguments from standard input. The last invocation of utility
77 shall be with fewer lines of arguments if fewer than number
78 remain. A line is considered to end with the first <newline>
79 unless the last character of the line is a <blank>; a trailing
80 <blank> signals continuation to the next non-empty line, inclu‐
81 sive. The -L and -n options are mutually-exclusive; the last one
82 specified shall take effect.
83
84 -n number
85 Invoke utility using as many standard input arguments as possi‐
86 ble, up to number (a positive decimal integer) arguments maxi‐
87 mum. Fewer arguments shall be used if:
88
89 * The command line length accumulated exceeds the size speci‐
90 fied by the -s option (or {LINE_MAX} if there is no -s
91 option).
92
93 * The last iteration has fewer than number, but not zero, oper‐
94 ands remaining.
95
96 -p Prompt mode: the user is asked whether to execute utility at
97 each invocation. Trace mode ( -t) is turned on to write the com‐
98 mand instance to be executed, followed by a prompt to standard
99 error. An affirmative response read from /dev/tty shall execute
100 the command; otherwise, that particular invocation of utility
101 shall be skipped.
102
103 -s size
104 Invoke utility using as many standard input arguments as possi‐
105 ble yielding a command line length less than size (a positive
106 decimal integer) bytes. Fewer arguments shall be used if:
107
108 * The total number of arguments exceeds that specified by the
109 -n option.
110
111 * The total number of lines exceeds that specified by the -L
112 option.
113
114 * End-of-file is encountered on standard input before size
115 bytes are accumulated.
116
117 Values of size up to at least {LINE_MAX} bytes shall be supported, pro‐
118 vided that the constraints specified in the DESCRIPTION are met. It
119 shall not be considered an error if a value larger than that supported
120 by the implementation or exceeding the constraints specified in the
121 DESCRIPTION is given; xargs shall use the largest value it supports
122 within the constraints.
123
124 -t Enable trace mode. Each generated command line shall be written
125 to standard error just prior to invocation.
126
127 -x Terminate if a command line containing number arguments (see the
128 -n option above) or number lines (see the -L option above)
129 will not fit in the implied or specified size (see the -s option
130 above).
131
132
134 The following operands shall be supported:
135
136 utility
137 The name of the utility to be invoked, found by search path
138 using the PATH environment variable, described in the Base Defi‐
139 nitions volume of IEEE Std 1003.1-2001, Chapter 8, Environment
140 Variables. If utility is omitted, the default shall be the echo
141 utility. If the utility operand names any of the special built-
142 in utilities in Special Built-In Utilities , the results are
143 undefined.
144
145 argument
146 An initial option or operand for the invocation of utility.
147
148
150 The standard input shall be a text file. The results are unspecified if
151 an end-of-file condition is detected immediately following an escaped
152 <newline>.
153
155 The file /dev/tty shall be used to read responses required by the -p
156 option.
157
159 The following environment variables shall affect the execution of
160 xargs:
161
162 LANG Provide a default value for the internationalization variables
163 that are unset or null. (See the Base Definitions volume of
164 IEEE Std 1003.1-2001, Section 8.2, Internationalization Vari‐
165 ables for the precedence of internationalization variables used
166 to determine the values of locale categories.)
167
168 LC_ALL If set to a non-empty string value, override the values of all
169 the other internationalization variables.
170
171 LC_COLLATE
172
173 Determine the locale for the behavior of ranges, equivalence
174 classes, and multi-character collating elements used in the
175 extended regular expression defined for the yesexpr locale key‐
176 word in the LC_MESSAGES category.
177
178 LC_CTYPE
179 Determine the locale for the interpretation of sequences of
180 bytes of text data as characters (for example, single-byte as
181 opposed to multi-byte characters in arguments and input files)
182 and the behavior of character classes used in the extended regu‐
183 lar expression defined for the yesexpr locale keyword in the
184 LC_MESSAGES category.
185
186 LC_MESSAGES
187 Determine the locale for the processing of affirmative responses
188 and that should be used to affect the format and contents of
189 diagnostic messages written to standard error.
190
191 NLSPATH
192 Determine the location of message catalogs for the processing of
193 LC_MESSAGES .
194
195 PATH Determine the location of utility, as described in the Base Def‐
196 initions volume of IEEE Std 1003.1-2001, Chapter 8, Environment
197 Variables.
198
199
201 Default.
202
204 Not used.
205
207 The standard error shall be used for diagnostic messages and the -t and
208 -p options. If the -t option is specified, the utility and its con‐
209 structed argument list shall be written to standard error, as it will
210 be invoked, prior to invocation. If -p is specified, a prompt of the
211 following format shall be written (in the POSIX locale):
212
213
214 "?..."
215
216 at the end of the line of the output from -t.
217
219 None.
220
222 None.
223
225 The following exit values shall be returned:
226
227 0 All invocations of utility returned exit status zero.
228
229 1-125 A command line meeting the specified requirements could not be
230 assembled, one or more of the invocations of utility returned a
231 non-zero exit status, or some other error occurred.
232
233 126 The utility specified by utility was found but could not be
234 invoked.
235
236 127 The utility specified by utility could not be found.
237
238
240 If a command line meeting the specified requirements cannot be assem‐
241 bled, the utility cannot be invoked, an invocation of the utility is
242 terminated by a signal, or an invocation of the utility exits with exit
243 status 255, the xargs utility shall write a diagnostic message and exit
244 without processing any remaining input.
245
246 The following sections are informative.
247
249 The 255 exit status allows a utility being used by xargs to tell xargs
250 to terminate if it knows no further invocations using the current data
251 stream will succeed. Thus, utility should explicitly exit with an
252 appropriate value to avoid accidentally returning with 255.
253
254 Note that input is parsed as lines; <blank>s separate arguments. If
255 xargs is used to bundle output of commands like find dir -print or ls
256 into commands to be executed, unexpected results are likely if any
257 filenames contain any <blank>s or <newline>s. This can be fixed by
258 using find to call a script that converts each file found into a quoted
259 string that is then piped to xargs. Note that the quoting rules used by
260 xargs are not the same as in the shell. They were not made consistent
261 here because existing applications depend on the current rules and the
262 shell syntax is not fully compatible with it. An easy rule that can be
263 used to transform any string into a quoted form that xargs interprets
264 correctly is to precede each character in the string with a backslash.
265
266 On implementations with a large value for {ARG_MAX}, xargs may produce
267 command lines longer than {LINE_MAX}. For invocation of utilities, this
268 is not a problem. If xargs is being used to create a text file, users
269 should explicitly set the maximum command line length with the -s
270 option.
271
272 The command, env, nice, nohup, time, and xargs utilities have been
273 specified to use exit code 127 if an error occurs so that applications
274 can distinguish "failure to find a utility" from "invoked utility
275 exited with an error indication". The value 127 was chosen because it
276 is not commonly used for other meanings; most utilities use small val‐
277 ues for "normal error conditions'' and the values above 128 can be con‐
278 fused with termination due to receipt of a signal. The value 126 was
279 chosen in a similar manner to indicate that the utility could be found,
280 but not invoked. Some scripts produce meaningful error messages differ‐
281 entiating the 126 and 127 cases. The distinction between exit codes 126
282 and 127 is based on KornShell practice that uses 127 when all attempts
283 to exec the utility fail with [ENOENT], and uses 126 when any attempt
284 to exec the utility fails for any other reason.
285
287 1. The following command combines the output of the parenthesised com‐
288 mands onto one line, which is then written to the end-of-file log:
289
290
291 (logname; date; printf "%s\n" "$0 $*") | xargs >>log
292
293 2. The following command invokes diff with successive pairs of argu‐
294 ments originally typed as command line arguments (assuming there
295 are no embedded <blank>s in the elements of the original argument
296 list):
297
298
299 printf "%s\n" "$*" | xargs -n 2 -x diff
300
301 3. In the following commands, the user is asked which files in the
302 current directory are to be archived. The files are archived into
303 arch; a, one at a time, or b, many at a time.
304
305
306 a. ls | xargs -p -L 1 ar -r arch
307
308
309 b. ls | xargs -p -L 1 | xargs ar -r arch
310
311 4. The following executes with successive pairs of arguments origi‐
312 nally typed as command line arguments:
313
314
315 echo $* | xargs -n 2 diff
316
317 5. On XSI-conformant systems, the following moves all files from
318 directory $1 to directory $2, and echoes each move command just
319 before doing it:
320
321
322 ls $1 | xargs -I {} -t mv $1/{} $2/{}
323
325 The xargs utility was usually found only in System V-based systems; BSD
326 systems included an apply utility that provided functionality similar
327 to xargs -n number. The SVID lists xargs as a software development
328 extension. This volume of IEEE Std 1003.1-2001 does not share the view
329 that it is used only for development, and therefore it is not optional.
330
331 The classic application of the xargs utility is in conjunction with the
332 find utility to reduce the number of processes launched by a simplistic
333 use of the find -exec combination. The xargs utility is also used to
334 enforce an upper limit on memory required to launch a process. With
335 this basis in mind, this volume of IEEE Std 1003.1-2001 selected only
336 the minimal features required.
337
338 Although the 255 exit status is mostly an accident of historical imple‐
339 mentations, it allows a utility being used by xargs to tell xargs to
340 terminate if it knows no further invocations using the current data
341 stream shall succeed. Any non-zero exit status from a utility falls
342 into the 1-125 range when xargs exits. There is no statement of how the
343 various non-zero utility exit status codes are accumulated by xargs.
344 The value could be the addition of all codes, their highest value, the
345 last one received, or a single value such as 1. Since no algorithm is
346 arguably better than the others, and since many of the standard utili‐
347 ties say little more (portably) than "pass/fail", no new algorithm was
348 invented.
349
350 Several other xargs options were withdrawn because simple alternatives
351 already exist within this volume of IEEE Std 1003.1-2001. For example,
352 the -i replstr option can be just as efficiently performed using a
353 shell for loop. Since xargs calls an exec function with each input
354 line, the -i option does not usually exploit the grouping capabilities
355 of xargs.
356
357 The requirement that xargs never produces command lines such that invo‐
358 cation of utility is within 2048 bytes of hitting the POSIX exec
359 {ARG_MAX} limitations is intended to guarantee that the invoked utility
360 has room to modify its environment variables and command line arguments
361 and still be able to invoke another utility. Note that the minimum
362 {ARG_MAX} allowed by the System Interfaces volume of
363 IEEE Std 1003.1-2001 is 4096 bytes and the minimum value allowed by
364 this volume of IEEE Std 1003.1-2001 is 2048 bytes; therefore, the 2048
365 bytes difference seems reasonable. Note, however, that xargs may never
366 be able to invoke a utility if the environment passed in to xargs comes
367 close to using {ARG_MAX} bytes.
368
369 The version of xargs required by this volume of IEEE Std 1003.1-2001 is
370 required to wait for the completion of the invoked command before
371 invoking another command. This was done because historical scripts
372 using xargs assumed sequential execution. Implementations wanting to
373 provide parallel operation of the invoked utilities are encouraged to
374 add an option enabling parallel invocation, but should still wait for
375 termination of all of the children before xargs terminates normally.
376
377 The -e option was omitted from the ISO POSIX-2:1993 standard in the
378 belief that the eofstr option-argument was recognized only when it was
379 on a line by itself and before quote and escape processing were per‐
380 formed, and that the logical end-of-file processing was only enabled if
381 a -e option was specified. In that case, a simple sed script could be
382 used to duplicate the -e functionality. Further investigation revealed
383 that:
384
385 * The logical end-of-file string was checked for after quote and
386 escape processing, making a sed script that provided equivalent
387 functionality much more difficult to write.
388
389 * The default was to perform logical end-of-file processing with an
390 underscore as the logical end-of-file string.
391
392 To correct this misunderstanding, the -E eofstr option was adopted from
393 the X/Open Portability Guide. Users should note that the description of
394 the -E option matches historical documentation of the -e option (which
395 was not adopted because it did not support the Utility Syntax Guide‐
396 lines), by saying that if eofstr is the null string, logical end-of-
397 file processing is disabled. Historical implementations of xargs actu‐
398 ally did not disable logical end-of-file processing; they treated a
399 null argument found in the input as a logical end-of-file string. (A
400 null string argument could be generated using single or double quotes (
401 '' or "" ). Since this behavior was not documented historically, it is
402 considered to be a bug.
403
405 None.
406
408 Shell Command Language , echo , find , the System Interfaces volume of
409 IEEE Std 1003.1-2001, exec
410
412 Portions of this text are reprinted and reproduced in electronic form
413 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
414 -- Portable Operating System Interface (POSIX), The Open Group Base
415 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
416 Electrical and Electronics Engineers, Inc and The Open Group. In the
417 event of any discrepancy between this version and the original IEEE and
418 The Open Group Standard, the original IEEE and The Open Group Standard
419 is the referee document. The original Standard can be obtained online
420 at http://www.opengroup.org/unix/online.html .
421
422
423
424IEEE/The Open Group 2003 XARGS(P)