386BSD 0.1 development
[unix-history] / usr / othersrc / public / shellutils-1.6 / man / cat1 / expr.0
EXPR(1L) EXPR(1L)
N\bNA\bAM\bME\bE
expr - evaluate expressions
S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
e\bex\bxp\bpr\br expression...
D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
This manual page documents the GNU version of e\bex\bxp\bpr\br. e\bex\bxp\bpr\br
evaluates an expression and writes the result on its stan-
dard output. Each token of the expression must be a sepa-
rate argument. Operands are either numbers or strings.
Strings are not quoted for e\bex\bxp\bpr\br, though you may need to
quote them to protect them from the shell. e\bex\bxp\bpr\br coerces
anything appearing in an operand position to an integer or
a string depending on the operation being applied to it.
The operators (in order of increasing precedence) are:
| yields its first argument if it is neither null nor
0, otherwise its second argument. This is the
usual `or' operation.
& yields its first argument if neither argument is
null or 0, otherwise 0.
< <= = != >= >
compare their arguments and return `1' if the rela-
tion is true, 0 otherwise. e\bex\bxp\bpr\br tries to coerce
both arguments to numbers and do a numeric compari-
son; if it fails when trying to coerce either argu-
ment it then does a lexicographic comparison.
+ - perform arithmetic operations. Both arguments are
coerced to numbers; an error occurs if this cannot
be done.
* / % perform arithmetic operations (`%' is the remainder
operation, as in C). Both arguments are coerced to
numbers; an error occurs if this cannot be done.
: performs pattern matching. Its arguments are
coerced to strings and the second one is considered
to be a regular expression, with a `^' implicitly
added at the beginning. The first argument is then
matched against this regular expression. If the
match succeeds and part of the string is enclosed
in `\(' and `\)', that part is the value of the :
expression; otherwise an integer whose value is the
number of characters matched is returned. If the
match fails, the : operator returns the null string
if `\(' and `\)' are used, otherwise 0. Only one
`\(' and `\)' pair can be used.
1
EXPR(1L) EXPR(1L)
Parentheses are used for grouping in the usual manner.
Examples:
To add 1 to the shell variable _\ba:
a=`expr $a + 1`
To find the filename part of the pathname stored in vari-
able _\ba, which may or may not contain `/':
expr $a : '.*/\(.*\)' '|' $a
Note the quoted shell metacharacters.
e\bex\bxp\bpr\br returns the following exit status:
0 if the expression is neither null nor 0,
1 if the expression is null or 0,
2 for invalid expressions.
2