PROLOGThis manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.
NAMEexpr --- evaluate arguments as an expression
DESCRIPTIONThe expr utility shall evaluate an expression and write the result to standard output.
OPERANDSThe single expression evaluated by expr shall be formed from the operand operands, as described in the EXTENDED DESCRIPTION section. The application shall ensure that each of the expression operator symbols:
( ) | & = > >= < <= != + - * / % :
ENVIRONMENT VARIABLESThe following environment variables shall affect the execution of expr:
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
- If set to a non-empty string value, override the values of all the other internationalization variables.
Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions and by the string comparison operators.
- 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.
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.
- Determine the location of message catalogs for the processing of LC_MESSAGES.
STDOUTThe expr utility shall evaluate the expression and write the result, followed by a <newline>, to standard output.
STDERRThe standard error shall be used only for diagnostic messages.
EXTENDED DESCRIPTIONThe formation of the expression to be evaluated is shown in the following table. The symbols expr, expr1, and expr2 represent expressions formed from integer and string 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 increasing precedence, with equal-precedence operators grouped between horizontal lines. All of the operators shall be left-associative.
|expr1 | expr2||
Returns the evaluation of
if it is neither null nor zero; otherwise, returns the evaluation of
if it is not null; otherwise, zero.
|expr1 & expr2||
Returns the evaluation of
if neither expression evaluates to null or zero; otherwise, returns zero.
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.
|expr1 = expr2||Equal.|
|expr1 > expr2||Greater than.|
|expr1 >= expr2||Greater than or equal.|
|expr1 < expr2||Less than.|
|expr1 <= expr2||Less than or equal.|
|expr1 != expr2||Not equal.|
|expr1 + expr2||
Addition of decimal integer-valued arguments.
|expr1 - expr2||
Subtraction of decimal integer-valued arguments.
|expr1 * expr2||
Multiplication of decimal integer-valued arguments.
|expr1 / expr2||
Integer division of decimal integer-valued arguments, producing
an integer result.
|expr1 % expr2||
Remainder of integer division of decimal integer-valued arguments.
|expr1 : expr2||
Matching expression; see below.
|( expr )||
Grouping symbols. Any expression can be placed within parentheses.
Parentheses can be nested to a depth of
An argument consisting only of an (optional) unary minus followed
A string argument; see below.
Matching ExpressionThe ':' matching operator shall compare the string resulting from the evaluation of expr1 with the regular expression pattern resulting from the evaluation of expr2. Regular expression syntax shall be that defined in the Base Definitions volume of POSIX.1-2008, Section 9.3, Basic Regular Expressions, 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 ('0' on failure). Alternatively, if the pattern contains at least one regular expression subexpression dq[\(...\)]dq, the string matched by the back-reference expression dq\1dq shall be returned. If the back-reference expression dq\1dq does not match, then the null string shall be returned.
String OperandA string argument is an argument that cannot be identified as an integer argument or as one of the expression operator symbols shown in the OPERANDS section. The use of string arguments length, substr, index, or match produces unspecified results.
EXIT STATUSThe following exit values shall be returned:
- The expression evaluates to neither null nor zero.
- The expression evaluates to null or zero.
- Invalid expression.
- An error occurred.
CONSEQUENCES OF ERRORSDefault.
The following sections are informative.
APPLICATION USAGEAfter argument processing by the shell, expr is not required to be able to tell the difference between an operator and an operand except by the value. If dq$adq is '=', the command:
expr $a = '='
expr = = =
expr X$a = X=
EXAMPLESThe expr 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
characters is required. For example:
In many cases, the arithmetic and string features provided as part of the shell command language are easier to use than their equivalents in Newly written scripts should avoidandThe following command: a=$(expr $a + 1) adds 1 to the variable The following command, for equal to either or just expr $a : '.*/\(.*\)' \| $a returns the last segment of a pathname (that is, Applications should avoid the character used alone as an argument; 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 variable and should be quoted to avoid having expand into multiple arguments. The following command: expr "$VAR" : '.*' returns the number of characters in 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. None. The Base Definitions volume of POSIX.1-2008, Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) 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 www.unix.org/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 www.kernel.org/doc/man-pages/reporting_bugs.html .