expr(1) expr(1)
expr - evaluate arguments as an expression
expr arguments
The arguments are taken as an expression. After evaluation, the result
is written on the standard output. Terms of the expression must be
separated by blanks. Characters special to the shell must be escaped.
Note that 0 is returned to indicate a zero value, rather than the null
string. Strings containing blanks or other special characters should be
quoted. Integer-valued arguments may be preceded by a unary minus sign.
Internally, integers are treated as 32-bit, 2s complement numbers. The
length of the expression is limited to 512 bytes. expr processes
supplementary code set characters according to the locale specified in
the LC_CTYPE environment variable [see LANG on environ(5)]. In regular
expressions, pattern searches are performed on characters, not bytes, as
described in regcomp(5).
The operators and keywords are listed below. Characters that need to be
escaped in the shell [see sh(1)] are preceded by \. The list is in order
of increasing precedence, with equal precedence operators grouped within
{} symbols.
expr1 <b>\| expr2
returns the evaluation of expr1 if it is neither null nor 0,
otherwise returns the evaluation of expr2.
expr1 <b>\& expr2
returns the evaluation of expr1 if neither expression evaluates to
null or 0, otherwise returns 0.
Returns the result of a decimal integer comparison if both arguments
are integers; otherwise, returns the result of a string comparison
using the locale-specific collation sequence. The result of each
comparison will be 1 if the specified relationship is true, or 0 if
the relationship is false.
expr1 { =, \>, \>=, \<, \<=, != } expr2
returns the result of an integer comparison if both arguments are
integers, otherwise returns the result of a lexical comparison.
expr1 { +, - } expr2
addition or subtraction of decimal integer-valued arguments.
expr1 { \*, /, % } expr2
multiplication, division, or remainder of the decimal integer-valued
arguments.
Page 1
expr(1) expr(1)
expr1 <b>: expr2
The matching operator : compares expr1 with expr2, which must be a
regular expression. Regular expression syntax is defined is defined
in the regcomp(5) man page under the section titled: Basic Regular
Expression, Normally, the matching operator returns the number of
bytes matched (0 on failure). Alternatively, if the pattern
contains at least one regular expression subexpression [\( . . .\)],
the string corresponding to \1 will be returned.
The use of string arguments length, substr, index or match produces
unspecified results.
Add 1 to the shell variable a:
a=`expr $a + 1`
The following example emulates basename(1)-it returns the last segment of
the path name $a. For $a equal to either /usr/abc/file or just file, the
example returns file. (Watch out for / alone as an argument: expr takes
it as the division operator; see the NOTES below.)
expr $a : '.*/\(.*\)' \| $a
Here is a better version of the previous example. The addition of the //
characters eliminates any ambiguity about the division operator and
simplifies the whole expression.
expr //$a : '.*/\(.*\)'
Return the number of characters in $VAR:
expr $VAR : '.*'
/usr/lib/locale/locale<b>/LC_MESSAGES/uxcore.abi
language-specific message file [See LANG on environ (5).]
regcomp(5), sh(1)
expr will evaluate the expression and write the result to standard
output. The character 0 will be written to indicate a zero value and
nothing will be written to indicate a null string.
Used only for diagnostic messages.
Page 2
expr(1) expr(1)
DIAGNOSTICS
As a side effect of expression evaluation, expr returns the following
exit values:
0 if the expression is neither null nor 0
1 if the expression is null or 0
2 for invalid expressions.
>2 An error occurred.
syntax error for operator/operand errors
non-numeric argument
if arithmetic is attempted on such a string
After argument processing by the shell, expr cannot tell the difference
between an operator and an operand except by the value. If $a is an =,
the command:
expr $a = '='
looks like:
expr = = =
as the arguments are passed to expr (and they are all taken as the =
operator). The following works:
expr X$a = X=
PPPPaaaaggggeeee 3333 [ Back ]
|