Open System Services Shell and Utilities Reference Manual (G06.25+, H06.03+)

expr(1) OSS Shell and Utilities Reference Manual
NAME
expr - Evaluates arguments as expressions
SYNOPSIS
expr expression
DESCRIPTION
The expr command reads an expression, evaluates it, and writes the result to the standard output
le. Within the expression argument, you must separate each term with spaces, precede characters
special to the shell with a \ (backslash), and quote strings containing spaces or other special char-
acters. Note that expr returns 0 (zero) to indicate a zero value, rather than the null string. Integers
can be preceded by a unary minus sign. Internally, integers are treated as 32-bit, twos comple-
ment 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 (zero); otherwise, returns expression2.
expression1 \&expression2
Returns expression1 if neither expression1 nor expression2 is null nor 0 (zero); other-
wise, returns 0 (zero).
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 argu-
ments.
expression1 : expression2
Compares expression1 with expression2, which must be a regular expression, with syn-
tax as described for the grep command, except that all patterns are anchored to the
beginning of the string, so ˆ (circumex) (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. Alterna-
tively, you can use the \(...\) symbols in expression2 to return a portion of expression1.
(expression)
Provides expression grouping.
To avoid unpredictable results when using a range expression to match a class of characters, use a
character class expression rather than a standard range expression. For information about charac-
ter class expressions, see the discussion of this topic in the grep command reference page.
Each part of an expression is composed of separate arguments, so use of spaces is required. For
example, enter the following command line instead of expr 1+2:
expr 1 + 2
3122 Hewlett-Packard Company 527188-003