expr(1) MPE/iX Shell and Utilities expr(1)
______________________________________________________________________
NAME
expr -- evaluate expression
SYNOPSIS
expr 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 arithme-
tic and string manipulation on shell variables.
expr recognizes the following operators. Operators listed
together have equal precedence; otherwise, they are in increasing
order of precedence. expr stores expressions as strings and con-
verts them to numbers 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, followed by 0 for octal, 0x for hexade-
cimal, otherwise decimal, followed by the digits of the number.
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, *[CMD expr] compares
them as numbers; otherwise it compares them as strings. If
the comparison is true, the expression results in 1; other-
wise 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.
1
expr(1) MPE/iX Shell and Utilities expr(1)
______________________________________________________________________
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 con-
sider ^ to be a metacharacter. If the regular expression
contains \(...\) and it matches at least part of expr1, then
expr results in only that part; if there is no match, expr
results in 0. If the regular expression doesn't contain
this construct, then 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.
EXAMPLES
fname=src/fn_abs.c
expr $fname : '.*_\(.*\)\.c'
returns abs.
a=`expr $a + 1`
adds one to the value of the shell variable a.
2
expr(1) MPE/iX Shell and Utilities expr(1)
______________________________________________________________________
DIAGNOSTICS
Possible exit status values are:
0 The result of expression is true.
1 The result of expression is false.
2 An error occurred.
Messages
Message: divide by 0
Cause: You attempted to divide a number by 0.
Action: Do not divide numbers by 0.
Message: internal tree error
Cause: You specified an expression that expr was unable to
evaluate, due to either syntax errors or unusual com-
plexity.
Action: Correct the syntax errors, or simplify the expression
(perhaps by breaking it into parts).
Message: no space for expression or string
Cause: There were not enough free system resources for expr to
allocate for a string or expression.
Action: Simplify the expression.
Message: non-numeric argument "string"
Cause: You specified a string argument with an operator that
requires a numeric argument (that is, +, -, *, /, or
%).
Action: Replace the string argument with a numeric argument.
Message: regular expression error "regexp"
Cause: An error occurred while processing the regular expres-
sion regexp.
Action: Check the regular expression.
Message: strcoll error, cannot malloc space.
Cause: There are not enough free system resources to allocate
string space.
Action: Free up more resources.
PORTABILITY
POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems.
In the shell, let(1) largely supercedes this command.
match, substr, length, and index are undocumented on all UNIX
systems, though they do appear to exist there. They are exten-
sions to the POSIX standard.
3
expr(1) MPE/iX Shell and Utilities expr(1)
______________________________________________________________________
MPE/iX NOTES
For information on how the current MPE/iX implementation may
affect the operation of this utility, see Appendix A, MPE/iX
Implementation Considerations.
SEE ALSO
ed(1), let(1), sh(1), test(1), regexp(3)
4