expr Command Purpose Evaluates arguments as expressions. Syntax expr Expression Description The expr command reads the Expression parameter, evaluates it, and writes the result to standard output. You must apply the following rules to the Expression parameter: * Separate each term with blanks. * Precede characters special to the shell with a backslash (\). * Quote strings containing blanks or other special characters. Integers may be preceded by a unary minus sign. Internally, in- tegers are treated as 32-bit, two's complement numbers. Note: The expr command returns 0 to indicate a zero value, rather than the null string. The following items describe Expression parameter operators and keywords. Characters that need to be escaped are preceded by a \ (backslash). The items are listed in order of increasing pre- cedence, with equal precedence operators grouped within {} (braces): Expression1 \| Expression2 Returns the Expression1 if it is nei- ther null nor 0; otherwise it returns Expression2. Expression1 \& Expression2 Returns Expression1 if neither Expression1 nor Expression2 is null or 0; otherwise it returns a value of 0. Expression1 { =, \>, \>=, \<, \<=, != } Expression2 Returns the result of an integer comparison if both expressions are in- tegers; otherwise returns the result of a string comparison. Expression1 {+, - } Expression2 Adds or subtracts integer-valued arguments. Expression1 { \*, /, % } Expression2 Multiplies, divides, or provides the remainder from the division of integer-valued argu- ments. Expression1 : Expression2 Compares Expression1 with Expression2, which must be a pattern. Pattern syntax is the same as that of the ed command, except that all patterns are anchored. The \^ (caret) symbol, which anchors a pattern to the beginning of a line, is not a special character in this context. Normally, the matching operator returns the number of characters matched. Al- ternatively, you can use the \( . . . \) symbols in Expression2 to return a portion of Expression1. In an expression such as [a-z], the minus means through according to the current collating sequence. A collating sequence can define equivalence classes for use in character ranges. See "Understanding Locale Environment Vari- ables" for more information on collating sequences and equivalence classes. Return Values The expr command returns the following exit values: 0 The expression is neither null nor 0 (zero). 1 The expression is null or 0 (zero). 2 The expression is not valid. Note: After parameter processing by the shell, the expr command cannot distinguish between an operator and an operand except by the value. Thus, if the value of $a is j, the command: expr $a = j looks like: expr j = j after the shell passes the arguments to the expr command. The following is also true: expr X$a = Xj Examples 1. To modify a shell variable, enter: COUNT=`expr $COUNT + 1` This adds 1 to the shell variable $COUNT. The expr command is enclosed in grave accents, which causes the shell to substitute the standard output from the expr command into the COUNT= com- mand. The $COUNT variable must be initialized before using. 2. To find the length of the $STR shell variable, enter: LENGTH=`expr $STR : ".*"` This sets the LENGTH variable to the value given by the: (colon) operator. The pattern .* matches any string from beginning to end, so the colon operator gives the length of the $STR variable as the number of characters matched. Note that ".*" must be within quotes to prevent the shell from treating the * (asterisk) as a pattern-matching character. The quotes are not part of the pattern. If the $STR variable is set to the null string, the command displays the error message expr: syntax error. This happens be- cause the shell does not normally pass null strings to commands. In this case the expr command sees only: :]..* The shell also removes the single quotation marks. This does not work because the colon operator requires two values. The problem is fixed by enclosing the shell variable in double quotation marks: LENGTH=`expr "$STR" : ".*"` Now if the value of the $STR variable is null, the LENGTH vari- able is set to a value of 0. Enclosing shell variables in double quotation marks is generally recommended. Do not enclose shell variables in single quotation marks. 3. To use part of a string, enter: FLAG=`expr "$FLAG" : "-*\(.*\)"` This removes leading minus signs, if any, from the shell variable $FLAG. The colon operator gives the part of the FLAG variable matched by the part of the pattern enclosed in \(].\). If you omit the \(].\), the colon operator gives the number of charac- ters matched. If the $FLAG variable is set to - (minus sign), the command displays a syntax error message. This happens because the shell substitutes the value of the $FLAG variable before running the expr command. The expr command does not know that the minus sign is the value of a variable. It can only see: - : -*\(.*\) and it interprets the first minus sign as the subtraction opera- tor. To eliminate this problem, use: FLAG=`expr "x$FLAG" : "x-*\(.*\)"` 4. To use the expr command in an if statement, enter: if expr "$ANSWER" : "[yY]" >/dev/null then echo ANSWER begins with "y" or "Y" fi If the $ANSWER variable begins with y or Y, the then part of the if statement is performed. If the match succeeds, the result of the expression is 1 and the expr command returns an exit value of 0, which is recognized as the logical value TRUE by the if statement. If the match fails, the result is 0 and the exit value 1 (FALSE). Redirecting the standard output of the expr command to the /dev/null special file discards the result of the expression. If you do not redirect it, the result is written to the standard output, which is usually your workstation display. 5. Consider the following expression: expr "$STR" = "=" If the $STR variable has the value = (equal sign), then after the shell processes this command the expr command sees the ex- pression: = = = The expr command interprets this as three = operators in a row and displays a syntax error message. This happens whenever the value of a shell variable is the same as that of one of the expr operators. You can avoid this problem by phrasing the expression as: expr "x$STR" = "x=" Implementation Specifics This command is part of Base Operating System (BOS) Runtime. File /usr/bin/expr Contains the expr command. Suggested Reading Prerequisite Information Commands Overview. Related Information National Language Support Overview for System Management. The bsh command, csh command, ed command, ksh command.