EXPR(1)                    POSIX Programmer's Manual                   EXPR(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
       expr -- evaluate arguments as an expression

SSYYNNOOPPSSIISS
       expr _o_p_e_r_a_n_d...

DDEESSCCRRIIPPTTIIOONN
       The _e_x_p_r utility shall evaluate an expression and write the result to
       standard output.

OOPPTTIIOONNSS
       None.

OOPPEERRAANNDDSS
       The single expression evaluated by _e_x_p_r shall be formed from the
       _o_p_e_r_a_n_d operands, as described in the EXTENDED DESCRIPTION section. The
       application shall ensure that each of the expression operator symbols:


           (  )  |  &  =  >  >=  <  <=  !=  +  -  *  /  %  :

       and the symbols _i_n_t_e_g_e_r and _s_t_r_i_n_g in the table are provided as
       separate arguments to _e_x_p_r.

SSTTDDIINN
       Not used.

IINNPPUUTT FFIILLEESS
       None.

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

       _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 within
                 regular expressions and by the string comparison operators.

       _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 the
                 behavior of character classes within regular expressions.

       _L_C___M_E_S_S_A_G_E_S
                 Determine the locale that should be used to affect the format
                 and contents of diagnostic messages 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.

AASSYYNNCCHHRROONNOOUUSS EEVVEENNTTSS
       Default.

SSTTDDOOUUTT
       The _e_x_p_r utility shall evaluate the expression and write the result,
       followed by a <newline>, to standard output.

SSTTDDEERRRR
       The standard error shall be used only for diagnostic messages.

OOUUTTPPUUTT FFIILLEESS
       None.

EEXXTTEENNDDEEDD DDEESSCCRRIIPPTTIIOONN
       The formation of the expression to be evaluated is shown in the
       following table. The symbols _e_x_p_r, _e_x_p_r_1, and _e_x_p_r_2 represent
       expressions formed from _i_n_t_e_g_e_r and _s_t_r_i_n_g symbols and the expression
       operator symbols (all separate arguments) by recursive application of
       the constructs described in the table. The expressions are listed in
       order of decreasing precedence, with equal-precedence operators grouped
       between horizontal lines. All of the operators shall be left-
       associative.

             +---------------+------------------------------------------+
             |  EExxpprreessssiioonn   |               DDeessccrriippttiioonn                |
             +---------------+------------------------------------------+
             |_i_n_t_e_g_e_r        | An argument consisting only of an        |
             |               | (optional) unary minus followed by       |
             |               | digits.                                  |
             |_s_t_r_i_n_g         | A string argument; see below.            |
             +---------------+------------------------------------------+
             |( _e_x_p_r )       | Grouping symbols. Any expression can be  |
             |               | placed within parentheses.  Parentheses  |
             |               | can be nested to a depth of              |
             |               | {EXPR_NEST_MAX}.                         |
             +---------------+------------------------------------------+
             |_e_x_p_r_1 : _e_x_p_r_2  | Matching expression; see below.          |
             +---------------+------------------------------------------+
             |_e_x_p_r_1 * _e_x_p_r_2  | Multiplication of decimal integer-valued |
             |               | arguments.                               |
             |_e_x_p_r_1 / _e_x_p_r_2  | Integer division of decimal integer-     |
             |               | valued arguments, producing an integer   |
             |               | result.                                  |
             |_e_x_p_r_1 % _e_x_p_r_2  | Remainder of integer division of decimal |
             |               | integer-valued arguments.                |
             +---------------+------------------------------------------+
             |_e_x_p_r_1 + _e_x_p_r_2  | Addition of decimal integer-valued       |
             |               | arguments.                               |
             |_e_x_p_r_1 - _e_x_p_r_2  | Subtraction of decimal integer-valued    |
             |               | arguments.                               |
             +---------------+------------------------------------------+
             |               | Returns the result of a decimal integer  |
             |               | comparison if both arguments are         |
             |               | integers; otherwise, returns the result  |
             |               | of a string comparison using the locale- |
             |               | specific collation sequence. The result  |
             |               | of each comparison is 1 if the specified |
             |               | relationship is true, or 0 if the        |
             |               | relationship is false.                   |
             |_e_x_p_r_1 = _e_x_p_r_2  | Equal.                                   |
             |_e_x_p_r_1 > _e_x_p_r_2  | Greater than.                            |
             |_e_x_p_r_1 >= _e_x_p_r_2 | Greater than or equal.                   |
             |_e_x_p_r_1 < _e_x_p_r_2  | Less than.                               |
             |_e_x_p_r_1 <= _e_x_p_r_2 | Less than or equal.                      |
             |_e_x_p_r_1 != _e_x_p_r_2 | Not equal.                               |
             +---------------+------------------------------------------+
             |_e_x_p_r_1 & _e_x_p_r_2  | Returns the evaluation of _e_x_p_r_1 if       |
             |               | neither expression evaluates to null or  |
             |               | zero; otherwise, returns zero.           |
             +---------------+------------------------------------------+
             |_e_x_p_r_1 | _e_x_p_r_2  | Returns the evaluation of _e_x_p_r_1 if it is |
             |               | neither null nor zero; otherwise,        |
             |               | returns the evaluation of _e_x_p_r_2 if it is |
             |               | not null; otherwise, zero.               |
             +---------------+------------------------------------------+
   MMaattcchhiinngg EExxpprreessssiioonn
       The ''::'' matching operator shall compare the string resulting from the
       evaluation of _e_x_p_r_1 with the regular expression pattern resulting from
       the evaluation of _e_x_p_r_2.  Regular expression syntax shall be that
       defined in the Base Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _9_._3,
       _B_a_s_i_c _R_e_g_u_l_a_r _E_x_p_r_e_s_s_i_o_n_s, except that all patterns are anchored to the
       beginning of the string (that is, only sequences starting at the first
       character of a string are matched by the regular expression) and,
       therefore, it is unspecified whether ''^^'' is a special character in that
       context. Usually, the matching operator shall return a string
       representing the number of characters matched (''00'' on failure).
       Alternatively, if the pattern contains at least one regular expression
       subexpression ""[[\\((......\\))]]"", the string matched by the back-reference
       expression ""\\11"" shall be returned. If the back-reference expression
       ""\\11"" does not match, then the null string shall be returned.

   IIddeennttiiffiiccaattiioonn aass IInntteeggeerr oorr SSttrriinngg
       An argument or the value of a subexpression that consists only of an
       optional unary minus followed by digits is a candidate for treatment as
       an integer if it is used as the left argument to the _| operator or as
       either argument to any of the following operators: _& _= _> _>_= _< _<_= _!_= _+ _-
       _* _/ _%.  Otherwise, the argument or subexpression value shall be treated
       as a string.

       The use of string arguments lleennggtthh, ssuubbssttrr, iinnddeexx, or mmaattcchh produces
       unspecified results.

EEXXIITT SSTTAATTUUSS
       The following exit values shall be returned:

        0    The _e_x_p_r_e_s_s_i_o_n evaluates to neither null nor zero.

        1    The _e_x_p_r_e_s_s_i_o_n evaluates to null or zero.

        2    Invalid _e_x_p_r_e_s_s_i_o_n.

       >2    An error occurred.

CCOONNSSEEQQUUEENNCCEESS OOFF EERRRROORRSS
       Default.

       _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 _e_x_p_r utility has a rather difficult syntax:

        *  Many of the operators are also shell control operators or reserved
           words, so they have to be escaped on the command line.

        *  Each part of the expression is composed of separate arguments, so
           liberal usage of <blank> characters is required. For example:

                       +-----------------+-----------------------+
                       |    IInnvvaalliidd      |         VVaalliidd         |
                       +-----------------+-----------------------+
                       |_e_x_p_r 1+2         | _e_x_p_r 1 + 2            |
                       |_e_x_p_r "1 + 2"     | _e_x_p_r 1 + 2            |
                       |_e_x_p_r 1 + (2 * 3) | _e_x_p_r 1 + \( 2 \* 3 \) |
                       +-----------------+-----------------------+
       In many cases, the arithmetic and string features provided as part of
       the shell command language are easier to use than their equivalents in
       _e_x_p_r.  Newly written scripts should avoid _e_x_p_r in favor of the new
       features within the shell; see _S_e_c_t_i_o_n _2_._5, _P_a_r_a_m_e_t_e_r_s _a_n_d _V_a_r_i_a_b_l_e_s
       and _S_e_c_t_i_o_n _2_._6_._4, _A_r_i_t_h_m_e_t_i_c _E_x_p_a_n_s_i_o_n.

       After argument processing by the shell, _e_x_p_r is not required to be able
       to tell the difference between an operator and an operand except by the
       value. If ""$$aa"" is ''=='', the command:


           expr "$a" = '='

       looks like:


           expr = = =

       as the arguments are passed to _e_x_p_r (and they all may be taken as the
       ''=='' operator). The following works reliably:


           expr "X$a" = X=

       Also note that this volume of POSIX.1-2017 permits implementations to
       extend utilities. The _e_x_p_r utility permits the integer arguments to be
       preceded with a unary minus. This means that an integer argument could
       look like an option.  Therefore, the conforming application must employ
       the ""----"" construct of Guideline 10 of 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 to protect its
       operands if there is any chance the first operand might be a negative
       integer (or any string with a leading minus).

       For testing string equality the _t_e_s_t utility is preferred over _e_x_p_r, as
       it is usually implemented as a shell built-in. However, the
       functionality is not quite the same because the _e_x_p_r _= and _!_= operators
       check whether strings collate equally, whereas _t_e_s_t checks whether they
       are identical. Therefore, they can produce different results in locales
       where the collation sequence does not have a total ordering of all
       characters (see the Base Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n
       _7_._3_._2, _L_C___C_O_L_L_A_T_E).

EEXXAAMMPPLLEESS
       The following command:


           a=$(expr "$a" + 1)

       adds 1 to the variable _a.

       The following command, for ""$$aa"" equal to either //uussrr//aabbcc//ffiillee or just
       ffiillee:


           expr $a : '.*/\(.*\)' \| $a

       returns the last segment of a pathname (that is, ffiillee).  Applications
       should avoid the character ''//'' used alone as an argument; _e_x_p_r may
       interpret it as the division operator.

       The following command:


           expr "//$a" : '.*/\(.*\)'

       is a better representation of the previous example. The addition of the
       ""////"" characters eliminates any ambiguity about the division operator
       and simplifies the whole expression. Also note that pathnames may
       contain characters contained in the _I_F_S variable and should be quoted
       to avoid having ""$$aa"" expand into multiple arguments.

       The following command:


           expr "X$VAR" : '.*' - 1

       returns the number of characters in _V_A_R.

RRAATTIIOONNAALLEE
       In an early proposal, EREs were used in the matching expression syntax.
       This was changed to BREs to avoid breaking historical applications.

       The use of a leading <circumflex> in the BRE is unspecified because
       many historical implementations have treated it as a special character,
       despite their system documentation. For example:


           expr foo : ^foo     expr ^foo : ^foo

       return 3 and 0, respectively, on those systems; their documentation
       would imply the reverse. Thus, the anchoring condition is left
       unspecified to avoid breaking historical scripts relying on this
       undocumented feature.

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _S_e_c_t_i_o_n _2_._5, _P_a_r_a_m_e_t_e_r_s _a_n_d _V_a_r_i_a_b_l_e_s, _S_e_c_t_i_o_n _2_._6_._4, _A_r_i_t_h_m_e_t_i_c
       _E_x_p_a_n_s_i_o_n

       The Base Definitions volume of POSIX.1-2017, _S_e_c_t_i_o_n _7_._3_._2, _L_C___C_O_L_L_A_T_E,
       _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 _9_._3, _B_a_s_i_c _R_e_g_u_l_a_r
       _E_x_p_r_e_s_s_i_o_n_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

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                              EXPR(1)
