Name
expr — stdin stdout - file -- opt --help --version
Synopsis
expr expression
The expr
command does simple
math (and other expression evaluation) on the command line:
➜expr 7 + 3
10 ➜expr '(' 7 + 3 ')' '*' 14
We quote special shell characters 140 ➜expr length ABCDEFG
7 ➜expr 15 '>' 16
0 Meaning false
Each argument must be separated by whitespace. Notice that we
had to quote or escape any characters that have special meaning to the
shell. Parentheses (escaped) may be used for grouping. Operators for
expr
include:
Operator | Numeric operation | String operation |
| Addition | |
| Subtraction | |
| Multiplication | |
| Integer division | |
| Remainder (modulo) | |
| Less than | Earlier in dictionary. |
| Less than or equal | Earlier in dictionary, or equal. |
| Greater than | Later in dictionary. |
| Greater than or equal | Later in dictionary, or equal. |
| Equality | Equality. |
| Inequality | Inequality. |
| Boolean “or” | Boolean “or”. |
| Boolean “and” | Boolean “and”. |
| Does the regular
expression |
In Boolean expressions, expr
treats the number 0 and the empty string as false; any other value is
true. When returning Boolean results, expr
returns 0 for false and 1 for
true.
expr
is not very efficient,
but it’s highly useful in shell scripts, described in Programming with Shell Scripts. For more complex needs,
consider using a language like Perl instead.
Get Macintosh Terminal Pocket Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.