expr(1) DG/UX 4.30 expr(1)
NAME
expr - evaluate arguments as an expression
SYNOPSIS
expr arguments
DESCRIPTION
The arguments are taken as an expression. After evaluation,
the result is written on the standard output. Terms of the
expression must be separated by blanks. Characters special
to the shell must be escaped. Note that 0 is returned to
indicate a zero value, rather than the null string. Strings
containing blanks or other special characters should be
enquoted. Integer-valued arguments may be preceded by a
unary minus sign. Internally, integers are treated as 32-
bit, 2s complement numbers.
The operators and keywords are listed below. Characters
that need to be escaped in the Bourne shell, sh(1), are
preceded by \. The list is in order of increasing
precedence, with equal precedence operators grouped within
{} symbols.
expr \| expr
Returns the first expr if it is neither null nor 0;
otherwise, returns the second expr.
expr \& expr
Returns the first expr if neither expr is null or 0;
otherwise, returns 0.
expr { =, \>, \>=, \<, \<=, != } expr
Returns the result of an integer comparison if both
arguments are integers; otherwise, returns the result
of a lexical comparison.
expr { +, - } expr
Adds or subtracts integer-valued arguments.
expr { \*, /, % } expr
Multiplies, divides, or gives remainder of the
integer-valued arguments.
expr : expr
The matching operator : compares the first argument
with the second argument. Both must be a regular
expression. Regular expression syntax is the same as
that of ed(1), except that all patterns are anchored
(i.e., begin with ^) and, therefore, ^ is not a special
character, in that context. Normally, the matching
operator returns the number of characters matched (0 on
failure). Alternatively, the \(...\) pattern symbols
Licensed material--property of copyright holder(s) Page 1
expr(1) DG/UX 4.30 expr(1)
can return a part of the first argument.
EXAMPLES
$ TEST=`expr $VAR1 \& $VAR2`
$ echo $TEST
0
Assuming VAR1 or VAR2 has a value of null or zero, the
variable TEST will get the value 0, as shown above. If VAR1
and VAR2 both had some value other than null or zero, the
value of VAR1 would have been assigned to TEST.
SEE ALSO
ed(1), sh(1).
EXIT CODE
As a side effect of expression evaluation, expr returns the
following exit values:
0 If the expression is neither null nor 0
1 If the expression is null or 0
2 For invalid expressions.
DIAGNOSTICS
syntax error For operator/operand errors
non-numeric argument
If arithmetic is attempted on such a string
WARNING
Remember that expr's arguments are first seen and processed
by the shell. Here are two examples of problems that can
arise:
1. Expr can only distinguish operators and operands
(arguments) by their value. Let's suppose that the current
value of shell variable a is a single equals sign (=). The
shell will read expr $a = '=' and turn it into expr = = =.
To avoid this kind of problem, attach a dummy character to
both your operands, like this: expr x$a = x=.
2. When a shell variable has a null value, the shell will
take the variable out of the command before expr gets to see
it. This is particularly troublesome when you want to use
\| and \&. For example, suppose shell variable b is null.
The shell will turn
expr $b \| 'test' into
expr \| test. To get around this problem, place double
quotes in the command line around shell variables that might
become null, like this:
expr "$b" \| 'test'.
Licensed material--property of copyright holder(s) Page 2