expr

evaluate expression

Command


SYNOPSIS

expr [-W] 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 arithmetic and string manipulation on KornShell variables.

Options

-W

on POSIX-compliant and UNIX systems, allows the use of octal and hexadecimal numbers.

Operators

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 converts 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 hexadecimal, 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, expr compares them as numbers; otherwise it compares them as strings. If the comparison is true, the expression results in 1; otherwise 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.

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. Note that the multiplication symbol (*) is expanded under the KornShell unless you specify it with a leading backslash (\\*), or enclosed in single quotes ('*') or double quotes ("*").

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 consider ^ 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 character position expr2 (origin 1) for the length of expr3 characters.

index expr1 expr2

searches for any of the characters in expr2 in expr1 and returns the first character position (origin 1) at which it finds such a character, or 0 if no such characters are found.

length expr1

returns the length of expr1 in characters.

( 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.


DIAGNOSTICS

Possible exit status values are:
0

The result of expression is true.

1

The result of expression is false.

2

Failure due to any of following:

— insufficient memory
— command line syntax error
— too few arguments on the command line
— invalid regular expression
— regular expression is too complicated
— non-numeric value found where a number was expected
internal tree error

Syntax errors or unusual expression complexity make it impossible for expr to evaluate an expression. If an expression has syntax errors, correct them; if not, simplify the expression (perhaps by breaking it into parts).


PORTABILITY

POSIX.2. x/OPEN Portability Guide 4.0.

In the KornShell, let largely supercedes this command.

match, substr, length, and index are undocumented on all UNIX systems, though they do appear to exist there. They are extensions to the POSIX and x/OPEN standards. The -W option is an extension to both the POSIX and x/OPEN standards.


LIMITS

The parser stack depth is limited to 150 levels. Attempting to process extremely complicated expressions may result in an overflow of this stack, causing an error.


SEE ALSO

Commands:
ed, let, sh, test

Miscellaneous:
regexp


Updated MKS Toolkit [3khat16.ico]HP3000 [3khat16.ico]3kMail [archive16.gif]