Evaluates arguments as expressions.
expr Expression
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:
Integers may be preceded by a unary hyphen. Internally, integers are treated as 32-bit, twos 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 precedence, with equal precedence operators grouped within { } (braces):
| Item | Description | 
|---|---|
| Expression1 \| Expression2 | Returns Expression1 if it is neither a null value nor a 0 value; otherwise, it returns Expression2. | 
| Expression1 \& Expression2 | Returns Expression1 if both expressions are neither a null value nor a 0 value; otherwise, it returns a value of 0. | 
| Expression1 { =, \>, \>=, \<, \<=, != } Expression2 | Returns the result of an integer comparison if both expressions are integers; otherwise, it 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 arguments. | 
| Expression1 : Expression2 | Compares the string resulting from the evaluation
of Expression1 with the regular expression pattern resulting
from the evaluation of Expression2. Regular expression syntax
is the same as that of the ed command, 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). Therefore, a ^ (caret) is not a special character in
this context. Normally the matching operator returns the number of characters matched (0 on failure). If the pattern contains a subexpression, that is: then a string containing the actual matched characters is returned. A collating sequence can define equivalence classes for use in character ranges. See "Understanding Locale Environment Variables" in AIX® Version 7.1 National Language Support Guide and Reference for more information on collating sequences and equivalence classes. | 
Note: The following string arguments are extensions beyond that of the standards, and the behavior may be different across operating systems. These string arguments are NOT portable.
| Item | Description | 
|---|---|
| match String1 String2 | Same as Expression1 : Expression2. | 
| length String1 | Returns the length of the String1. | 
| index String1 String2 | Returns the first position in String1 where any character in String2 exists. | 
| substr String1 StartPosition Length | Returns a string that starts with the character at StartPosition in String1 and continuies for Length characters | 
This command returns the following exit values:
| Item | Description | 
|---|---|
| 0 | The Expression parameter evaluates to neither null nor 0. | 
| 1 | The Expression parameter evaluates to null or 0. | 
| 2 | The Expression parameter is not valid. | 
| >2 | An error occurred. | 
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 = jlooks like:
expr j = jafter the shell passes the arguments to the expr command. The following is also true:
expr X$a = XjCOUNT=`expr $COUNT + 1`LENGTH=`expr $STR : ".*"`If the $STR variable is set to the null string or contains any white space (blanks or tabs), then the command displays the error message expr: syntax error. This happens because the shell does not normally pass null strings to commands. In this case, the expr command sees only:
:.*LENGTH=`expr "$STR" : ".*"`FLAG=`expr "$FLAG" : "-*\(.*\)"`If the $FLAG variable is set to - (hyphen), 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 hyphen is the value of a variable. It can only see:
- : -*\(.*\)FLAG=`expr "x$FLAG" : "x-*\(.*\)"`if expr "$ANSWER" : "[yY]" >/dev/null
then
echo ANSWER begins with "y" or "Y"
fiRedirecting 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.
expr "$STR" = "="= = =expr "x$STR" = "x="expr length $SHELL12expr index abcdef de4expr index abcdef fd4expr substr "Goodnight Ladies" 11 6Ladies| Item | Description | 
|---|---|
| /usr/bin/expr | Contains the expr command. |