maux
Star on GitHub Buy me a coffee

Manual / expr

expr(1)

EXPR(1) General Commands Manual EXPR(1)

expr - evaluate arguments as an expression

expr arg ...

The arguments are taken as an expression. After evaluation, the result is written on the standard output. Each token of the expression is a separate argument.

The operators are listed in order of increasing precedence, with equal precedence operators grouped:

|
Yields the first operand if it is neither null nor 0, otherwise the second.
&
Yields the first operand if neither is null or 0, otherwise 0.
< <= = != >= >
Yield 1 if the comparison is true, 0 if false. Comparison is numeric if both operands are numbers, otherwise lexical.
+ -
Addition or subtraction.
* / %
Multiplication, integer division, or remainder.
:
The matching operator compares the first argument with the second, which must be a basic regular expression, as in ed(1). The match is anchored at the start of the first argument. Normally the matching operator returns the number of characters matched (0 on failure). Alternatively, if the expression contains a \(...\) group, the operator returns the text of the first group (the null string on failure or if the group took no part in the match). A group that is a plain decimal integer may be used as a number. A malformed regular expression matches nothing.

Shell metacharacters must be quoted. For example:

expr 2 + 3 '*' 4

prints 14, and

expr /usr/src/expr.s : '.*/\(.*\)\.s'

prints expr, the part of the name after the last slash.

/usr/man/EXPR this page

sh(1), licenses(1)

Exit status is 0 if the result is neither null nor 0; 1 if it is null or 0; 2 for syntax, type or range errors; 3 for division or remainder by zero.

Arithmetic is unsigned 16-bit (0 through 65535). Negative results and overflow are errors. The string functions (match, substr, index, length) are not implemented. A regular expression of 255 bytes or more is an error (status 2).

October 8, 2026 maux