expr operand
The expr utility shall evaluate an expression and write the result to standard output.
None.
The single expression evaluated by expr shall be formed from the operands, as described in the EXTENDED DESCRIPTION section. The application shall ensure that each of the expression operator symbols:
( ) | & = > >= < <= != + - * / % :
and the symbols integer and string in the table are provided as separate arguments to expr.
Not used.
None.
The following environment variables shall affect the execution of expr:
Determine the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions and by the string comparison operators.
Default.
The expr utility shall evaluate the expression and write the result, followed by a <newline>, to standard output.
The standard error shall be used only for diagnostic messages.
None.
The 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 expr1 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 {EXPR_NEST_MAX}. |
integer | An argument consisting only of an (optional) unary minus followed by digits. |
string | A string argument; see below. |
The ’:’ 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 IEEE Std 1003.1-2001, 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 "[\(...\)]" , the string corresponding to "\1" shall be returned.
A 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.
The following exit values shall be returned:
Default.
The following sections are informative.
After 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 "$a" is ’=’ , the command:
expr $a = ’=’
looks like:
expr = = =
as the arguments are passed to expr (and they all may be taken as the ’=’ operator). The following works reliably:
expr X$a = X=
Also note that this volume of IEEE Std 1003.1-2001 permits implementations to extend utilities. The expr 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 IEEE Std 1003.1-2001, Section 12.2, Utility Syntax Guidelines to protect its operands if there is any chance the first operand might be a negative integer (or any string with a leading minus).
The 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 <blank>s 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 expr. Newly written scripts should avoid expr in favor of the new features within the shell; see Parameters and
Rationale
Future Directions
See Also
Copyright