Name
expr
Synopsis
exprarg1
operator
arg2
[operator
arg3
..
. ]
Evaluate arguments as expressions and print the results. Arguments and operators must be separated by spaces. In most cases, an argument is an integer, typed literally or represented by a shell variable. There are three types of operators: arithmetic, relational, and logical, as well as keyword expressions. Exit status for expr is 0 (expression is nonzero and nonnull), 1 (expression is 0 or null), or 2 (expression is invalid).
Arithmetic operators
Use these to produce mathematical expressions whose results are printed:
- +
Add arg2 to arg1.
- -
Subtract arg2 from arg1.
- *
Multiply the arguments.
- /
Divide arg1 by arg2.
- %
Take the remainder when arg1 is divided by arg2.
Addition and subtraction are evaluated last, unless they are grouped inside parentheses. The symbols *, (, and ) have meaning to the shell, so they must be escaped (preceded by a backslash or enclosed in single quotes).
Relational operators
Use these to compare two arguments. Arguments can also be words, in which case comparisons are defined by the locale. If the comparison statement is true, the result is 1; if false, the result is 0. Symbols > and < must be escaped.
- =, = =
Are the arguments equal?
- !=
Are the arguments different?
- >
Is arg1 greater than arg2?
- >=
Is arg1 greater than or equal to arg2?
- <
Is arg1 less than arg2?
- <=
Is arg1 less than or equal to arg2?
Logical operators
Use these to compare two arguments. Depending on the values, the result can ...
Get Linux in a Nutshell, Fourth Edition 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.