exprevaluate expression |
Command |
expr
[-W
]
expression
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.
-W
on POSIX-compliant and UNIX systems, allows the use of octal and hexadecimal numbers.
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.
results in the value expr1 if expr1 is true; otherwise it results in the value of expr2.
results in the value of expr1 if both expressions are true; otherwise it results in 0
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.
performs addition or subtraction on the two expressions. If either
expression is not a number, expr
exits with an
error.
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 ("*"
).
match
expr1 rematches 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 expr3results in the substring of expr1 starting at character position expr2 (origin 1) for the length of expr3 characters.
index
expr1 expr2searches 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
expr1returns the length of expr1 in characters.
groups expressions.
returnsfname=src/fn_abs.c expr $fname : '.*_\(.*\)\.c'
abs
.
adds one to the value of the shell variablea=`expr $a + 1`
a
.
0
The result of expression is true.
1
The result of expression is false.
2
Failure due to any of following:
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).
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.
regexp