| expr | |
|---|---|
Example of expr command that does basic summation | |
| Original authors | Dick Haight (AT&T Bell Laboratories) |
| Developers | Variousopen-source andcommercial developers |
| Initial release | 1979; 47 years ago (1979) |
| Operating system | Unix,Unix-like,IBM i |
| Platform | Cross-platform |
| Type | Command |
expr is ashellcommand that evaluates anexpression and outputs the result. It evaluatesinteger orstring expressions, including pattern matchingregular expressions. Comparison operators (equal, not equal, less than, etc.) apply to both integer and string values. Integer-specific operations include addition, subtraction, multiplication, division and modulus. String-specific operators include:
match, or the colon syntax "str" : "reg" for an arbitrary string "str" and regular expression "reg")index)substr)length)Each symbol (operator, value, etc.) in the expression must be given as a separate parameter. A common challenge with using the command is preventing the shell from acting on input indented to be the expression as something other than expression.
Originally developed forUnix v7 and standardized byPOSIX, the command is available onIBM i,[1] and forWindows viaUnxUtils.[2][3]
The following is a (non-POSIX-compliant) example involvingBoolean expressions:
expr length "abcdef" "<" 5 "|" 15 - 4 ">" 8
This example outputs "1". This is because length "abcdef" is 6, which is not less than 5 (so the left side of the | returns zero). But 15 minus 4 is 11 and is greater than 8, so the right side is true, which makes theor true, so 1 is the result. The programexit status is zero for this example.
For pure arithmetic, it is often more convenient to usebc. For example:
echo "3 * 4 + 14 / 2" | bc
since it accepts the expression as a single argument.
Forportable shell programming, use of the "index", "length", "match" and "substr" commands must be avoided; string matching remains possible but it must use the "string : regexp" syntax.
expr: evaluate arguments as an expression – Shell and Utilities Reference,The Single UNIX Specification, Version 5 fromThe Open Group