Format
expr –W expression
Description
The set of arguments passed
to expr constitutes an expression
to be evaluated. Each
command argument is a separate token of the expression. expr writes
the result of the expression on the standard output. This command
is primarily intended for arithmetic and string manipulation on shell
variables.
expr supports
the following operators. Operators explained together have equal precedence;
otherwise, they are in increasing order of precedence.
expr stores
an expression as a string and converts it to a number during the operation.
If the context requires a Boolean value, a numeric value of
0 (zero)
or a null string ("") is
false, and any other value is
true.
Numbers have an optional leading sign. If the
-W option
is not specified, numbers are decimal. If the
-W option is
specified, expressions may contain octal, hexadecimal, or decimal
numbers.
expr determines the base of the
number as follows:
- Any number that starts with 0x is hexadecimal.
- Any number that starts with 0 is octal.
- Any number that does not start with 0x or 0 is decimal.
Numbers are manipulated as long integers.
- expr1 | expr2
- Results in the value expr1 if expr1 is
true; otherwise, it results in the value of expr2.
- expr1 & expr2
- Results in the value of expr1 if both
expressions are true; otherwise, it results in 0.
- expr1 <= expr2 | expr1 < expr2 | expr1 = expr2 | expr1 != expr2 | expr1 >= expr2 | expr1 > expr2
- If both expr1 and expr2 are
numeric, expr compares them as numbers;
otherwise, it compares them as strings. If the comparison is true,
the expression results in 1; otherwise, it results
in 0.
- expr1 + expr2 | expr1 – expr2
- Performs addition or subtraction on the two expressions. If either
expression is not a number, expr exits with
an error.
- expr1 * expr2 | expr1 / expr2 | expr1 % expr2
- Performs multiplication, division, or modulus on the two expressions.
If either expression is not a number, expr exits
with an error.
- expr1 : re | match expr1 re
- matches the regular expression re against expr1 treated
as a string. The regular expression is the same as that accepted by ed,
except that the match is always anchored—that is, there is an implied
leading ^. Therefore, expr does
not consider ^ to be a metacharacter. If the regular
expression contains \(…\), \) and
it matches at least part of expr1, expr results
in only that part; if there is no match, expr results
in 0. If the regular expression doesn't contain this
construct, the result is the number of characters matched. The function match performs
the same operation as the colon operator.
- substr expr1 expr2 expr3
- Results in the substring of expr1 starting
at position expr2 (origin 1) for the length
of expr3.
- index expr1 expr2
- Searches for any of the characters in expr2 in expr1 and
results in the offset of any such character (origin 1), or 0 if
no such characters are found.
- length expr1
- Results in the length of expr1.
- (expr)
- Groups expressions.
Options
- –W
- Allows the expression to use hexadecimal
and octal numbers.
Usage notes
The parser stack depth is limited
to 150 levels. Attempting to process extremely complicated expressions
may result in an overflow of this stack, causing an error.
Examples
- The example
fname=src/fn_abs.c
expr $fname : '*_\(.*\)\.c'
returns abs.
- The example
a='expr $a + 1'
adds 1 to
the value of the shell variable a.
Localization
expr uses
the following localization environment variables:
- LANG
- LC_ALL
- LC_COLLATE
- LC_CTYPE
- LC_MESSAGES
- LC_SYNTAX
- NLSPATH
See Localization for more
information.
Exit values
- 0
- The result of expression is true.
- 1
- The result of expression is false.
- 2
- Failure due to any of following:
- Not enough memory.
- Command-line syntax error.
- Too few arguments on the command line.
- Incorrect regular expression.
- Regular expression is too complicated.
- Nonnumeric value found where a number was expected.
Messages
Possible error messages include:
- internal tree error
- Syntax errors or unusual expression complexity make it impossible
for expr to evaluate an expression. If an
expression has syntax errors, correct them; if not, simplify the expression
(perhaps by breaking it into parts).
Portability
POSIX.2, X/Open Portability Guide, UNIX systems.
In
the shell, let largely supersedes this command.
match, substr, length,
and index are not documented on all UNIX systems,
though they do appear to exist. They are extensions of the POSIX standard.