expr
PURPOSE
Evaluates arguments as an expression.
SYNOPSIS
expr arguments
DESCRIPTION
The expr command reads an expression, evaluates it, and
writes the result to standard output. Within expression,
you must separate each term with blanks, precede charac-
ters special to the shell with a backslash (\), and quote
strings containing blanks or other special characters.
Note that expr returns 0 to indicate a zero value, rather
than the null string. Integers may be preceded by a
unary minus sign. Internally, integers are treated as
32-bit, two's complement numbers.
The operators and keywords are described in the following
listing. Characters that need to be escaped are preceded
by a backslash (\). The list is in order of increasing
precedence, with equal precedence operators grouped
within braces ({}).
expression1 \| expression2
Returns expression1 if it is neither null nor
0; otherwise it returns expression2.
expression1 \"&" expression2
Returns expression1 if neither expression1 nor
expression2 is null or 0; otherwise it returns
0.
expression1 "{ =, \>, \>=, \<, \<=, != }"
expression2
Returns the result of an integer comparison if
both expressions are integers; otherwise
returns the result of a string comparison.
expression1 "{+, - }" expression2
Adds or subtracts integer-valued arguments.
expression1 "{ \*, /, % }" expression2
Multiplies, divides, or provides the remainder
from the division of integer-valued arguments.
expression1 : expression2
Compares expression1 with expression2, which
must be a pattern; pattern syntax is the same
as that of the ed command (see page 352),
except that all patterns are anchored, so ^
(which anchors a pattern to the beginning of a
line), is not a special character in this
context.
Normally, the matching operator returns the
number of characters matched. Alternatively,
you can use the \( . . . \) symbols in
expression2 to return a portion of expression1.
In an expression such as [a-z], the minus means
"through" according to the current collating
sequence. A collating sequence may define
equivalence classes for use in character
ranges. See the "Overview of International
Character Support" in Managing the AIX Oper-
ating System for more information on collating
sequences and equivalence classes.
The expr command returns the following exit values:
0 The expression is neither null nor 0.
1 The expression is null or 0.
2 The expression is invalid.
Note: After parameter processing by the shell, expr
cannot distinguish between an operator and an operand
except by the value. Thus, if "$a" is "=", the command:
expr $a = '='
looks like:
expr = = =
after the shell passes the arguments to expr, and they
will all be taken as the = operator. The following
works:
expr X$a = X=
EXAMPLES
1. To modify a shell variable:
COUNT=`expr $COUNT + 1`
This adds "1" to the shell variable "COUNT". The
expr command is enclosed in grave accents, which
causes the shell to substitute the standard output
from expr into the "COUNT=" command. For more
details, see "Command Substitution."
2. To find the length of a shell variable:
LENGTH=`expr $STR : ".*"`
This sets "LENGTH" to the value given by the ":"
(colon) operator. The pattern ".*" matches any
string from beginning to end, so the colon operator
gives the length of "STR" as the number of characters
matched. Note that "".*"" must be in quotes to
prevent the shell from treating the "*" as a pattern-
matching character. The quotes themselves are not
part of the pattern.
If "STR" is set to the null string, the error message
"expr: syntax error" is displayed. This happens
because the shell does not normally pass null strings
to commands. In other words, the expr command sees
only
: .*
(The shell also removes the quotation marks.) This
does not work because the colon operator requires two
values. We can fix this problem by enclosing the
shell variable in double quotation marks:
LENGTH=`expr "$STR" : ".*"`
Now if "STR" is null, "LENGTH" is set to zero.
Enclosing shell variables in double quotes is recom-
mended in general. However, do not enclose shell
variables in single quotes. See page for details
about quoting.
3. To use part of a string:
FLAG=`expr "$FLAG" : "-*\(.*\)"`
This removes leading minus signs, if any, from the
shell variable "FLAG". The colon operator gives the
part of "FLAG" matched by the part of the pattern
enclosed in \( \). If you omit the \( \), the colon
operator gives the number of characters matched.
If "FLAG" is set to - (minus), a syntax error message
is displayed. This happens because the shell substi-
tutes the value of "FLAG" before running the expr
command. expr does not know that the minus is the
value of a variable. It can only see:
- : -*\(.*\)
and it interprets the first minus sign as the sub-
traction operator. We can fix this problem by using:
FLAG=expr "x$FLAG" : "x-*\(.*\)"
4. To use expr in an if statement:
if expr "$ANSWER" : "[yY]" >/dev/null
then
# ANSWER begins with "y" or "Y"
fi
If "ANSWER" begins with "y" or "Y", the then part of
the if statement is performed. If the match suc-
ceeds, the result of the expression is "1" and expr
returns an exit value of "0", which is recognized as
the logical value TRUE by if. If the match fails,
the result is "0" and the exit value "1" (FALSE).
Redirecting the standard output of expr to the
/dev/null special file discards the result of the
expression. If you do not redirect it, the result is
written to the standard output, which is usually your
work station display.
5. Consider the following expression:
expr "$STR" = "="
If "STR" has the value "=" (equal sign), then after
the shell processes this command expr sees the
expression:
= = =
The expr command interprets this as three "=" opera-
tors in a row and displays a syntax error message.
This happens whenever the value of a shell variable
is the same as one of the expr operators. You can
avoid this problem by doing the following:
expr "x$STR" = "x="
RELATED INFORMATION
The following commands: "ed" and "sh."
The "Overview of International Character Support" in Man-
aging the AIX Operating System.