Name
expr
Synopsis
exprarg1 operator arg2
[operator arg3...
]
Evaluates arguments as expressions and prints the result. Strings can
be compared and searched. 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. Exit status for
expr
is 0 (expression is nonzero and nonnull), 1
(expression is 0 or null), or 2 (expression is invalid).
expr
is typically used in shell scripts to perform
simple mathematics, such as addition or subtraction. It is made
obsolete in the Korn shell by that program’s
built-in arithmetic capabilities.
Arithmetic Operators
Use the following operators to produce mathematical expressions whose results are printed:
-
+
Add
arg2
toarg1
.-
-
Subtract
arg2
fromarg1
.-
*
Multiply the arguments.
-
/
Divide
arg1
byarg2
.-
%
Take the remainder when
arg1
is divided byarg2
.
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 or double quotes).
Relational operators
Use relational operators to compare two
arguments. Arguments can also be words, in which case comparisons
assume a
<
z
and A
<
Z
. 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?
-
>
Get Mac OS X Panther in a Nutshell, 2nd 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.