.TH expr "" "" Command
.PC "Compute a command-line expression"
\fBexpr \fIargument ...\fR
.PP
.HS
.SH Options:
.IC \fBn\fR
Any integer with optional sign
.IC \fIstring\fR
Used with comparisons and \fBlen\fR operator
.IC \fB+\fR
Arithmetic operators (one of `+', `-', `*', `/', `%')
.IC \fB!\fR
Unary not
.IC \fB-\fR
Unary minus
.IC \fB==\fR
Relational operators (one of `>', `<', `>=', `<=', `==', `!=')
.IC \fB&\fR
Logical AND of previous and next expression
.IC \fB|\fR
Logical OR of previous and next expression
.IC \fBlen\fR
Length of string given by next argument
.IC \fIe1\fB:\fIe2\fR
Set to number of characters matching
regular expression \fIe2\fR in string \fIe1\fR;
if \fIe2\fR contains any `\e(...\e)' sequences,
result is concatenation of matched parts
.IC "\fB( \fIe \fB)\fR"
Parentheses for grouping
.IC \fB{ \fIe1 \fB, \fIe2 \fB, \fIe3 \fB}\fR
.br
Evaluate \fIe2\fR if \fIe1\fR is true, \fIe3\fR
otherwise; \fIe3\fR defaults to 0 if missing
.HE
The arguments to
.B expr
form an expression.
.B expr
evaluates the expression and writes the result on the standard output.
Among other uses,
.B expr
lets the user perform arithmetic in shell command files.
.PP
Each
.I argument
is a separate token in the expression.
An argument has a logical value \*(Qlfalse\*(Qr
if it is a null string or has numerical value zero, \*(Qltrue\*(Qr
otherwise.
Integer arguments consist of an optional sign followed by a string of
decimal digits.
The range of valid integers is that of signed long integers.
No check is made for overflow or illegal arithmetic operations.
Floating point numbers are not supported.
.PP
The following list gives each
.B expr
operator and its meaning.
The list is in order of increasing operator precedence;
operators of the same precedence are grouped together.
All operators associate left to right except
the unary operators \*(Ql!\*(Qr, \*(Ql\-\*(Qr, and \*(Ql\fBlen\fR\*(Qr,
which associate right to left.
The spaces shown are significant \- they separate the tokens of the expression.
.RS
.IP "\fB{ \fIexpr1\fB, \fIexpr2\fB, \fIexpr3\fB }\fR"
.br
Return
.I expr2
if
.I expr1
is logically true, and
.I expr3
otherwise.
Alternatively,
\fB{ \fIexpr1 \fB,\fI expr2 \fB}\fR
is equivalent to
\fB{ \fIexpr1 \fB, \fIexpr2 \fB, 0 }\fR.
.IP "\fIexpr1 \fB|\fI expr2\fR"
.br
Return
.I expr1
if it is true,
.I expr2
otherwise.
.IP "\fIexpr1 \fB&\fI expr2\fR"
.br
Return
.I expr1
if both are true, zero otherwise.
.IP "\fIexpr1 relation expr2\fR"
.br
Where
.I relation
is one of <, <=, >, >=, ==, or !=,
return one if the
.I relation
is true, zero otherwise.
The comparison is numeric if both arguments
can be interpreted as numbers,
lexicographic otherwise.
The lexicographic comparison is the same as
.B strcmp
(see
.BR string ).
.IP "\fIexpr1\fB +\fI expr2\fR"
.IP "\fIexpr1\fB \-\fI expr2\fR"
.br
Add or subtract the integer arguments.
The expression is invalid if either
.I expr
is not a number.
.IP "\fIexpr1\fB * \fIexpr2\fR"
.IP "\fIexpr1\fB / \fIexpr2\fR"
.IP "\fIexpr1\fB % \fIexpr2\fR"
.br
Multiply, divide, or take remainder of the arguments.
The expression is invalid if either
.I expr
is not numeric.
.IP "\fIexpr1\fB : \fIexpr2\fR"
.br
Match patterns (regular expressions).
.I expr2
specifies a pattern in the syntax used by
.BR ed .
It is compared to
.IR expr1 ,
which may be any string.
If the \e(...\e) pattern occurs in the regular expression
the matching operator returns the matched field from the string;
if there is more than one \e(...\e) pattern the extracted
fields are concatenated in the result.
Otherwise, the matching operator returns
the number of characters matched.
.IP "\fBlen\fI expr\fR"
.br
Return the length of
.IR expr .
It behaves like
.B strlen
(see
.BR string ).
.I len
is a reserved word in
.BR expr .
.IP "\fB!\fIexpr\fR"
Perform logical negation:
return zero if
.I expr
is true, one otherwise.
.IP "\fB\-\fIexpr\fR"
Unary minus:
return the negative of its integer argument.
If the argument is non-numeric the expression is invalid.
.IP "\fB( \fIexpr\fB )\fR
.br
Return the
.IR expr .
The parentheses allow grouping expressions in any desired way.
.PP
The following operators have special meanings to the shell
.BR sh ,
and must be quoted to be interpreted correctly:
.BR "{ } ( ) < > & | *" .
.SH "See Also"
.Xr "commands," commands
.Xr "ed," ed
.Xr "ksh," ksh
.Xr "sh," sh
.Xr "test" test
.SH Notes
.B expr
returns zero if the expression is true,
one if false,
and two if an error occurs.
In the latter case an error message is also printed.
