XARGS(1)                   POSIX Programmer's Manual                  XARGS(1)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       xargs -- construct argument lists and invoke utility

SSYYNNOOPPSSIISS
       xargs [[-ptx]] [[-E _e_o_f_s_t_r]] [[-I _r_e_p_l_s_t_r|-L _n_u_m_b_e_r|-n _n_u_m_b_e_r]]
           [[-s _s_i_z_e]] [[_u_t_i_l_i_t_y [[_a_r_g_u_m_e_n_t...]]]]

DDEESSCCRRIIPPTTIIOONN
       The _x_a_r_g_s utility shall construct a command line consisting of the
       _u_t_i_l_i_t_y and _a_r_g_u_m_e_n_t operands specified followed by as many arguments
       read in sequence from standard input as fit in length and number
       constraints specified by the options. The _x_a_r_g_s utility shall then
       invoke the constructed command line and wait for its completion. This
       sequence shall be repeated until one of the following occurs:

        *  An end-of-file condition is detected on standard input.

        *  An argument consisting of just the logical end-of-file string (see
           the --EE _e_o_f_s_t_r option) is found on standard input after double-quote
           processing, <apostrophe> processing, and <backslash>-escape
           processing (see next paragraph). All arguments up to but not
           including the argument consisting of just the logical end-of-file
           string shall be used as arguments in constructed command lines.

        *  An invocation of a constructed command line returns an exit status
           of 255.

       The application shall ensure that arguments in the standard input are
       separated by unquoted <blank> characters, unescaped <blank> characters,
       or <newline> characters. A string of zero or more non-double-quote
       (''""'') characters and non-<newline> characters can be quoted by
       enclosing them in double-quotes. A string of zero or more
       non-<apostrophe> (''\\'''') characters and non-<newline> characters can be
       quoted by enclosing them in <apostrophe> characters. Any unquoted
       character can be escaped by preceding it with a <backslash>.  The
       utility named by _u_t_i_l_i_t_y shall be executed one or more times until the
       end-of-file is reached or the logical end-of file string is found. The
       results are unspecified if the utility named by _u_t_i_l_i_t_y attempts to
       read from its standard input.

       The generated command line length shall be the sum of the size in bytes
       of the utility name and each argument treated as strings, including a
       null byte terminator for each of these strings. The _x_a_r_g_s utility shall
       limit the command line length such that when the command line is
       invoked, the combined argument and environment lists (see the _e_x_e_c
       family of functions in the System Interfaces volume of POSIX.1-2017)
       shall not exceed {ARG_MAX}-2048 bytes. Within this constraint, if
       neither the --nn nor the --ss option is specified, the default command line
       length shall be at least {LINE_MAX}.

OOPPTTIIOONNSS
       The _x_a_r_g_s utility shall conform to the Base Definitions volume of
       POSIX.1-2017, _S_e_c_t_i_o_n _1_2_._2, _U_t_i_l_i_t_y _S_y_n_t_a_x _G_u_i_d_e_l_i_n_e_s.

       The following options shall be supported:

       --EE _e_o_f_s_t_r Use _e_o_f_s_t_r as the logical end-of-file string. If --EE is not
                 specified, it is unspecified whether the logical end-of-file
                 string is the <underscore> character (''__'') or the end-of-file
                 string capability is disabled. When _e_o_f_s_t_r is the null
                 string, the logical end-of-file string capability shall be
                 disabled and <underscore> characters shall be taken
                 literally.

       --II _r_e_p_l_s_t_r
                 Insert mode: _u_t_i_l_i_t_y is executed for each logical line from
                 standard input. Arguments in the standard input shall be
                 separated only by unescaped <newline> characters, not by
                 <blank> characters. Any unquoted unescaped <blank> characters
                 at the beginning of each line shall be ignored. The resulting
                 argument shall be inserted in _a_r_g_u_m_e_n_t_s in place of each
                 occurrence of _r_e_p_l_s_t_r.  At least five arguments in _a_r_g_u_m_e_n_t_s
                 can each contain one or more instances of _r_e_p_l_s_t_r.  Each of
                 these constructed arguments cannot grow larger than an
                 implementation-defined limit greater than or equal to 255
                 bytes. Option --xx shall be forced on.

       --LL _n_u_m_b_e_r The _u_t_i_l_i_t_y shall be executed for each non-empty _n_u_m_b_e_r lines
                 of arguments from standard input. The last invocation of
                 _u_t_i_l_i_t_y shall be with fewer lines of arguments if fewer than
                 _n_u_m_b_e_r remain. A line is considered to end with the first
                 <newline> unless the last character of the line is an
                 unescaped <blank>; a trailing unescaped <blank> signals
                 continuation to the next non-empty line, inclusive.

       --nn _n_u_m_b_e_r Invoke _u_t_i_l_i_t_y using as many standard input arguments as
                 possible, up to _n_u_m_b_e_r (a positive decimal integer) arguments
                 maximum. Fewer arguments shall be used if:

                  *  The command line length accumulated exceeds the size
                     specified by the --ss option (or {LINE_MAX} if there is no
                     --ss option).

                  *  The last iteration has fewer than _n_u_m_b_e_r, but not zero,
                     operands remaining.

       --pp        Prompt mode: the user is asked whether to execute _u_t_i_l_i_t_y at
                 each invocation. Trace mode (--tt) is turned on to write the
                 command instance to be executed, followed by a prompt to
                 standard error. An affirmative response read from //ddeevv//ttttyy
                 shall execute the command; otherwise, that particular
                 invocation of _u_t_i_l_i_t_y shall be skipped.

       --ss _s_i_z_e   Invoke _u_t_i_l_i_t_y using as many standard input arguments as
                 possible yielding a command line length less than _s_i_z_e (a
                 positive decimal integer) bytes. Fewer arguments shall be
                 used if:

                  *  The total number of arguments exceeds that specified by
                     the --nn option.

                  *  The total number of lines exceeds that specified by the
                     --LL option.

                  *  End-of-file is encountered on standard input before _s_i_z_e
                     bytes are accumulated.

                 Values of _s_i_z_e up to at least {LINE_MAX} bytes shall be
                 supported, provided that the constraints specified in the
                 DESCRIPTION are met. It shall not be considered an error if a
                 value larger than that supported by the implementation or
                 exceeding the constraints specified in the DESCRIPTION is
                 given; _x_a_r_g_s shall use the largest value it supports within
                 the constraints.

       --tt        Enable trace mode. Each generated command line shall be
                 written to standard error just prior to invocation.

       --xx        Terminate if a constructed command line will not fit in the
                 implied or specified size (see the --ss option above).

OOPPEERRAANNDDSS
       The following operands shall be supported:

       _u_t_i_l_i_t_y   The name of the utility to be invoked, found by search path
                 using the _P_A_T_H environment variable, described in the Base
                 Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t
                 _V_a_r_i_a_b_l_e_s.  If _u_t_i_l_i_t_y is omitted, the default shall be the
                 _e_c_h_o utility. If the _u_t_i_l_i_t_y operand names any of the special
                 built-in utilities in _S_e_c_t_i_o_n _2_._1_4, _S_p_e_c_i_a_l _B_u_i_l_t_-_I_n
                 _U_t_i_l_i_t_i_e_s, the results are undefined.

       _a_r_g_u_m_e_n_t  An initial option or operand for the invocation of _u_t_i_l_i_t_y.

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

IINNPPUUTT FFIILLEESS
       The file //ddeevv//ttttyy shall be used to read responses required by the --pp
       option.

EENNVVIIRROONNMMEENNTT VVAARRIIAABBLLEESS
       The following environment variables shall affect the execution of
       _x_a_r_g_s:

       _L_A_N_G      Provide a default value for the internationalization
                 variables that are unset or null. (See the Base Definitions
                 volume of POSIX.1-2017, _S_e_c_t_i_o_n _8_._2, _I_n_t_e_r_n_a_t_i_o_n_a_l_i_z_a_t_i_o_n
                 _V_a_r_i_a_b_l_e_s for the precedence of internationalization
                 variables used to determine the values of locale categories.)

       _L_C___A_L_L    If set to a non-empty string value, override the values of
                 all the other internationalization variables.

       _L_C___C_O_L_L_A_T_E
                 Determine the locale for the behavior of ranges, equivalence
                 classes, and multi-character collating elements used in the
                 extended regular expression defined for the yyeesseexxpprr locale
                 keyword in the _L_C___M_E_S_S_A_G_E_S category.

       _L_C___C_T_Y_P_E  Determine the locale for the interpretation of sequences of
                 bytes of text data as characters (for example, single-byte as
                 opposed to multi-byte characters in arguments and input
                 files) and the behavior of character classes used in the
                 extended regular expression defined for the yyeesseexxpprr locale
                 keyword in the _L_C___M_E_S_S_A_G_E_S category.

       _L_C___M_E_S_S_A_G_E_S
                 Determine the locale used to process affirmative responses,
                 and the locale used to affect the format and contents of
                 diagnostic messages and prompts written to standard error.

       _N_L_S_P_A_T_H   Determine the location of message catalogs for the processing
                 of _L_C___M_E_S_S_A_G_E_S.

       _P_A_T_H      Determine the location of _u_t_i_l_i_t_y, as described in the Base
                 Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t
                 _V_a_r_i_a_b_l_e_s.

AASSYYNNCCHHRROONNOOUUSS EEVVEENNTTSS
       Default.

SSTTDDOOUUTT
       Not used.

SSTTDDEERRRR
       The standard error shall be used for diagnostic messages and the --tt and
       --pp options. If the --tt option is specified, the _u_t_i_l_i_t_y and its
       constructed argument list shall be written to standard error, as it
       will be invoked, prior to invocation. If --pp is specified, a prompt of
       the following format shall be written (in the POSIX locale):


           "?..."

       at the end of the line of the output from --tt.

OOUUTTPPUUTT FFIILLEESS
       None.

EEXXTTEENNDDEEDD DDEESSCCRRIIPPTTIIOONN
       None.

EEXXIITT SSTTAATTUUSS
       The following exit values shall be returned:

           0   All invocations of _u_t_i_l_i_t_y returned exit status zero.

       1-125   A command line meeting the specified requirements could not be
               assembled, one or more of the invocations of _u_t_i_l_i_t_y returned a
               non-zero exit status, or some other error occurred.

         126   The utility specified by _u_t_i_l_i_t_y was found but could not be
               invoked.

         127   The utility specified by _u_t_i_l_i_t_y could not be found.

CCOONNSSEEQQUUEENNCCEESS OOFF EERRRROORRSS
       If a command line meeting the specified requirements cannot be
       assembled, the utility cannot be invoked, an invocation of the utility
       is terminated by a signal, or an invocation of the utility exits with
       exit status 255, the _x_a_r_g_s utility shall write a diagnostic message and
       exit without processing any remaining input.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       The 255 exit status allows a utility being used by _x_a_r_g_s to tell _x_a_r_g_s
       to terminate if it knows no further invocations using the current data
       stream will succeed. Thus, _u_t_i_l_i_t_y should explicitly _e_x_i_t with an
       appropriate value to avoid accidentally returning with 255.

       Note that since input is parsed as lines, <blank> characters separate
       arguments, and <backslash>, <apostrophe>, and double-quote characters
       are used for quoting, if _x_a_r_g_s is used to bundle the output of commands
       like _f_i_n_d _d_i_r --pprriinntt or _l_s into commands to be executed, unexpected
       results are likely if any filenames contain <blank>, <newline>, or
       quoting characters. This can be solved by using find to call a script
       that converts each file found into a quoted string that is then piped
       to _x_a_r_g_s, but in most cases it is preferable just to have _f_i_n_d do the
       argument aggregation itself by using --eexxeecc with a ''++'' terminator
       instead of '';;''.  Note that the quoting rules used by _x_a_r_g_s are not the
       same as in the shell. They were not made consistent here because
       existing applications depend on the current rules. An easy (but
       inefficient) method that can be used to transform input consisting of
       one argument per line into a quoted form that _x_a_r_g_s interprets
       correctly is to precede each non-<newline> character with a
       <backslash>.  More efficient alternatives are shown in Example 2 and
       Example 5 below.

       On implementations with a large value for {ARG_MAX}, _x_a_r_g_s may produce
       command lines longer than {LINE_MAX}.  For invocation of utilities,
       this is not a problem. If _x_a_r_g_s is being used to create a text file,
       users should explicitly set the maximum command line length with the --ss
       option.

       The _c_o_m_m_a_n_d, _e_n_v, _n_i_c_e, _n_o_h_u_p, _t_i_m_e, and _x_a_r_g_s utilities have been
       specified to use exit code 127 if an error occurs so that applications
       can distinguish ``failure to find a utility'' from ``invoked utility
       exited with an error indication''. The value 127 was chosen because it
       is not commonly used for other meanings; most utilities use small
       values for ``normal error conditions'' and the values above 128 can be
       confused with termination due to receipt of a signal. The value 126 was
       chosen in a similar manner to indicate that the utility could be found,
       but not invoked. Some scripts produce meaningful error messages
       differentiating the 126 and 127 cases. The distinction between exit
       codes 126 and 127 is based on KornShell practice that uses 127 when all
       attempts to _e_x_e_c the utility fail with [[EENNOOEENNTT]], and uses 126 when any
       attempt to _e_x_e_c the utility fails for any other reason.

EEXXAAMMPPLLEESS
        1. The following command combines the output of the parenthesized
           commands (minus the <apostrophe> characters) onto one line, which
           is then appended to the file log. It assumes that the expansion of
           ""$$00$*" does not include any <apostrophe> or <newline> characters.


               (logname; date; printf "'%s'\n$0 $*") | xargs -E "" >>log

        2. The following command invokes _d_i_f_f with successive pairs of
           arguments originally typed as command line arguments. It assumes
           there are no embedded <newline> characters in the elements of the
           original argument list.


               printf "%s\n$@" | sed 's/[^[:alnum:]]/\\&/g' |
                   xargs -E "" -n 2 -x diff

        3. In the following commands, the user is asked which files in the
           current directory (excluding dotfiles) are to be archived. The
           files are archived into aarrcchh; _a, one at a time or _b, many at a
           time. The commands assume that no filenames contain <blank>,
           <newline>, <backslash>, <apostrophe>, or double-quote characters.


               a. ls | xargs -E "" -p -L 1 ar -r arch

               b. ls | xargs -E "" -p -L 1 | xargs -E "" ar -r arch

        4. The following command invokes _c_o_m_m_a_n_d_1 one or more times with
           multiple arguments, stopping if an invocation of _c_o_m_m_a_n_d_1 has a
           non-zero exit status.


               xargs -E "" sh -c 'command1 "$@" || exit 255' sh < xargs_input

        5. On XSI-conformant systems, the following command moves all files
           from directory $$11 to directory $$22, and echoes each move command
           just before doing it. It assumes no filenames contain <newline>
           characters and that neither $$11 nor $$22 contains the sequence ""{{}}"".


               ls -A "$1" | sed -e 's/"/"\\""/g' -e 's/.*/"&"/' |
                   xargs -E "" -I {} -t mv "$1"/{} "$2"/{}

RRAATTIIOONNAALLEE
       The _x_a_r_g_s utility was usually found only in System V-based systems; BSD
       systems included an _a_p_p_l_y utility that provided functionality similar
       to _x_a_r_g_s --nn _n_u_m_b_e_r.  The SVID lists _x_a_r_g_s as a software development
       extension. This volume of POSIX.1-2017 does not share the view that it
       is used only for development, and therefore it is not optional.

       The classic application of the _x_a_r_g_s utility is in conjunction with the
       _f_i_n_d utility to reduce the number of processes launched by a simplistic
       use of the _f_i_n_d --eexxeecc combination. The _x_a_r_g_s utility is also used to
       enforce an upper limit on memory required to launch a process. With
       this basis in mind, this volume of POSIX.1-2017 selected only the
       minimal features required.

       Although the 255 exit status is mostly an accident of historical
       implementations, it allows a utility being used by _x_a_r_g_s to tell _x_a_r_g_s
       to terminate if it knows no further invocations using the current data
       stream shall succeed. Any non-zero exit status from a utility falls
       into the 1-125 range when _x_a_r_g_s exits. There is no statement of how the
       various non-zero utility exit status codes are accumulated by _x_a_r_g_s.
       The value could be the addition of all codes, their highest value, the
       last one received, or a single value such as 1. Since no algorithm is
       arguably better than the others, and since many of the standard
       utilities say little more (portably) than ``pass/fail'', no new
       algorithm was invented.

       Several other _x_a_r_g_s options were removed because simple alternatives
       already exist within this volume of POSIX.1-2017. For example, the --ii
       _r_e_p_l_s_t_r option can be just as efficiently performed using a shell ffoorr
       loop. Since _x_a_r_g_s calls an _e_x_e_c function with each input line, the --ii
       option does not usually exploit the grouping capabilities of _x_a_r_g_s.

       The requirement that _x_a_r_g_s never produces command lines such that
       invocation of _u_t_i_l_i_t_y is within 2048 bytes of hitting the POSIX _e_x_e_c
       {ARG_MAX} limitations is intended to guarantee that the invoked utility
       has room to modify its environment variables and command line arguments
       and still be able to invoke another utility. Note that the minimum
       {ARG_MAX} allowed by the System Interfaces volume of POSIX.1-2017 is
       4096 bytes and the minimum value allowed by this volume of POSIX.1-2017
       is 2048 bytes; therefore, the 2048 bytes difference seems reasonable.
       Note, however, that _x_a_r_g_s may never be able to invoke a utility if the
       environment passed in to _x_a_r_g_s comes close to using {ARG_MAX} bytes.

       The version of _x_a_r_g_s required by this volume of POSIX.1-2017 is
       required to wait for the completion of the invoked command before
       invoking another command. This was done because historical scripts
       using _x_a_r_g_s assumed sequential execution. Implementations wanting to
       provide parallel operation of the invoked utilities are encouraged to
       add an option enabling parallel invocation, but should still wait for
       termination of all of the children before _x_a_r_g_s terminates normally.

       The --ee option was omitted from the ISO POSIX-2:1993 standard in the
       belief that the _e_o_f_s_t_r option-argument was recognized only when it was
       on a line by itself and before quote and escape processing were
       performed, and that the logical end-of-file processing was only enabled
       if a --ee option was specified. In that case, a simple _s_e_d script could
       be used to duplicate the --ee functionality. Further investigation
       revealed that:

        *  The logical end-of-file string was checked for after quote and
           escape processing, making a _s_e_d script that provided equivalent
           functionality much more difficult to write.

        *  The default was to perform logical end-of-file processing with an
           <underscore> as the logical end-of-file string.

       To correct this misunderstanding, the --EE _e_o_f_s_t_r option was adopted from
       the X/Open Portability Guide. Users should note that the description of
       the --EE option matches historical documentation of the --ee option (which
       was not adopted because it did not support the Utility Syntax
       Guidelines), by saying that if _e_o_f_s_t_r is the null string, logical end-
       of-file processing is disabled.  Historical implementations of _x_a_r_g_s
       actually did not disable logical end-of-file processing; they treated a
       null argument found in the input as a logical end-of-file string. (A
       null _s_t_r_i_n_g argument could be generated using single or double-quotes
       ('''' or """").  Since this behavior was not documented historically, it is
       considered to be a bug.

       The --II, --LL, and --nn options are mutually-exclusive. Some implementations
       use the last one specified if more than one is given on a command line;
       other implementations treat combinations of the options in different
       ways.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _C_h_a_p_t_e_r _2, _S_h_e_l_l _C_o_m_m_a_n_d _L_a_n_g_u_a_g_e, _d_i_f_f, _e_c_h_o, _f_i_n_d

       The Base Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t
       _V_a_r_i_a_b_l_e_s, _S_e_c_t_i_o_n _1_2_._2, _U_t_i_l_i_t_y _S_y_n_t_a_x _G_u_i_d_e_l_i_n_e_s

       The System Interfaces volume of POSIX.1-2017, _e_x_e_c

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                             XARGS(1)
