Manual Page Result
0
Command: expr | Section: 1 | Source: Digital UNIX | File: expr.1.gz
expr(1) General Commands Manual expr(1)
NAME
expr - Evaluates arguments as expressions
SYNOPSIS
expr expression
STANDARDS
Interfaces documented on this reference page conform to industry stan-
dards as follows:
expr: XPG4, XPG4-UNIX
Refer to the standards(5) reference page for more information about in-
dustry standards and associated tags.
OPTIONS
None
OPERANDS
Expression to be evaluated, formed as explained in the DESCRIPTION sec-
tion.
DESCRIPTION
The expr command reads an expression, evaluates it, and writes the re-
sult to standard output. Within the expression argument, you must sep-
arate each term with spaces, precede characters special to the shell
with a \ (backslash), and quote strings containing spaces or other spe-
cial characters. Note that expr returns 0 to indicate a zero value,
rather than the null string. Integers can be preceded by a unary minus
sign. Internally, integers are treated as 64-bit or 32-bit two's com-
plement numbers, depending on the word size of the hardware platform.
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 op-
erators grouped within {} (braces). Returns expression1 if it is nei-
ther null nor 0 (zero); otherwise, returns expression2. Returns ex-
pression1 if neither expression1 nor expression2 is null nor 0; other-
wise, returns 0. Returns the result of an integer comparison if both
expressions are integers; otherwise, returns the result of a string
comparison. Adds or subtracts integer-valued arguments. Multiplies,
divides, or provides the remainder from the division of integer-valued
arguments. Compares expression1 with expression2, which must be a reg-
ular expression, with syntax as described for grep, except that all
patterns are anchored, so ^ (circumflex) (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 ex-
pression2 to return a portion of expression1. Provides expres-
sion 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 character class ex-
pressions, see the discussion of this topic included in the description
of the grep command.
FUNCTIONS
[DIGITAL] The expr command provides the following string-expression
functions. Strings containing white space must be quoted. The first
character of a string is position 1. The information returned by all
of these functions is in the form of a string: [DIGITAL] Compares each
character in the second string against each character in the first
string and returns the position in the first string of the first match
found, where the first match is the match closest to the beginning of
string1. [DIGITAL] Returns the length of the string argument. [DIGI-
TAL] See the description of the : (colon) match operator, above.
[DIGITAL] Returns the substring of string that begins at character po-
sition start and is length characters long.
EXIT STATUS
The expr command returns the following exit values: The expression is
neither null nor 0. The expression is null or 0. The expression is
invalid. An error occurred.
EXAMPLES
To increment a shell variable, enter: COUNT=`expr $COUNT + 1`
This adds 1 to the COUNT shell variable (see sh for details).
To find the length of a shell variable, enter: RES=`expr "$VAR"
: ".*"`
Note that VAR is in double quotes to avoid problems where VAR is
NULL or contains embedded spaces. The regular expression is
also quoted to avoid expansion by the shell. This operation can
also be performed as: RES=`expr length "$VAR"` To use part of a
shell variable, enter: RES=`expr "$VAR" : "-*\(.*\)"`
This removes leading - (dashes), if any, from VAR. If the \( \)
characters were omitted, RES would contain the length of VAR.
To find a character from one string in another string, enter:
INDEX=`expr index "Hello World!" "Wo"`
Note that the returned value is 5, not 7. The first match is
the fifth character in string1, matching the o in string2 rather
than the W, which is the seventh character in string1. Special
considerations: RES=`expr "x$VAR" : "x-*\(.*\)"`
This succeeds even if VAR has the value - (dash). RES=`expr
"x$VAR" = "x="
This succeeds even if VAR has the value = (equal sign).
ENVIRONMENT VARIABLES
The following environment variables affect the execution of expr: Pro-
vides a default value for the internationalization variables that are
unset or null. If LANG is unset or null, the corresponding value from
the default locale is used. If any of the internationalization vari-
ables contain an invalid setting, the utility behaves as if none of the
variables had been defined. If set to a non-empty string value, over-
rides the values of all the other internationalization variables. De-
termines the locale for the interpretation of sequences of bytes of
text data as characters (for example, single-byte as opposed to multi-
byte characters in arguments) and the behavior of character classes in
regular expressions.. Determines the locale for the format and con-
tents of diagnostic messages written to standard error. Determines the
location of message catalogues for the processing of LC_MESSAGES.
SEE ALSO
Commands: grep(1), Bourne shell sh(1b), POSIX shell sh(1p), test(1)
Functions: string(3), wcscat(3)
Standards: standards(5)
expr(1)