expr -- evaluate arguments as an expression


expr arguments


expr evaluates its arguments as an expression and writes the result on the standard output. An expression that evaluates to a null string returns a newline.

expr is useful for performing variable arithmetic and other variable manipulation within shell scripts. The ``Examples'' section contains some ideas for the use of expr.

The expressions must conform to the following rules:

Parentheses, (), can be used for grouping; see the ``Examples'' section for the syntax.

The operators and keywords are listed in order of increasing precedence, with equal precedence operators grouped within curly braces { }:

arg | arg
Returns the evaluation of the first arg if it is neither null nor 0, otherwise returns the evaluation of the second arg.

arg & arg
Returns the evaluation of the first arg if neither arg is null nor 0, otherwise returns 0.

arg { =, ==, >, >=, <, <=, != } arg
Returns the result of an integer comparison if both arguments are integers, otherwise returns the result of a lexical comparison, as defined by the current locale. The result will be 1 if the expression is true and 0 if the expression is false. The double equals sign (==) does the same thing as the single equals sign (=); it is simply an alternative syntax.

arg { *, /, % } arg
Multiplication, division, or remainder of the integer-valued arguments.

arg { +, - } arg
Addition or subtraction of integer-valued arguments.

arg : arg
The matching operator (:) compares the string resulting from the evaluation of the first argument with the regular expression pattern resulting from evaluation of the second argument. The regular expression syntax is the same as that of ed(C), except that all patterns are ``anchored'' (that is, begin with a caret (^)) and therefore the caret is not a special character in that context. Normally the matching operator returns the number of characters matched (0 on failure). Alternatively, the \(...\) grouping operators can be used; expr returns the first grouped subexpression of the first argument (equivalent to \1; see regexp(M) for further information about basic regular expressions.)

match string rexp
The match operator is identical in function to the colon operator (:) described above, but with a different syntax.

substr string x y
The substring operator takes three arguments: a string, an integer index into the string, x; and the number of characters to return from the string, y. substr goes to the xth character in string and returns the next y characters. If y is greater than the number of remaining characters in the string, expr will return the remainder of the string. x must be an integer greater than 0; y must be a positive integer (0 is acceptable, if you want 0 as the result). See the following section for an example.

length string
The length operator returns the length (the number of characters) of string.

index string r [stuv]
The index operator returns an integer indicating the place of r in string. If r is not in string, 0 is returned. You can specify as many characters as you like in the second argument; expr will then take the first character which appears in string and return its place in the string as an integer. See the following section for an example.

Exit values

As a side effect of expression evaluation, expr returns the following exit values:

if the expression is neither null nor zero

if the expression is null or zero

for invalid expressions

an error occurred


syntax error
for operator/operand errors, including unset variables

nonnumeric argument
if arithmetic is attempted on a nonnumeric string


This is an example of how expr can be used in a shell script to do variable arithmetic:
   a=`expr $a + 1`
   echo $a
Parentheses can be placed around the part of an expression you want evaluated first. Be careful with the syntax; the backslashes and whitespace are essential:
   expr \( 1 + 2 \) \* 10
The matching operator in expr (: or match) can be used to return a portion of a pathname:
   expr $a : '.*/\(.*\)'
basename(C) does the same thing, however, and uses a simpler syntax:
   basename $a
You can use the length operator to check the length of a string variable, and assign this value to another variable:
   b=`expr length $a`
   echo $b
The substring (substr) operator pulls out a specific part of a string:
   expr substr mongoose 4 7
Here, the expr substring operator returns a substring of ``mongoose'' specified by 4 (start from the fourth character) and 7 (give me the next seven characters). Note that there are not seven more characters in ``mongoose'' from the ``g'', so expr only returns what is left.

The index operator tells you the place of a character in a string:

   expr index wombat zoqb
In this example, the index operator takes the ``o'', the first character that is actually in the string ``wombat'', and returns its place in the string. expr index wombat o would have the same result.


expr has a tricky syntax; you are recommended to use a liberal amount of whitespace.

Many expr operators are interpreted as metacharacters by the shell unless they are escaped using backslash or single quotes.

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 equals sign (=), the command:

   expr  $a  =  "="
looks like:
   expr  =  =  =
The arguments are passed to expr and will all be taken as the = operator. The following permits comparing equals signs:
   expr  X$a  =  X=

See also

awk(C), basename(C), bc(C), dd(C), locale(M), sh(C)

Standards conformance

expr is conformant with:

ISO/IEC DIS 9945-2:1992, Information technology - Portable Operating System Interface (POSIX) - Part 2: Shell and Utilities (IEEE Std 1003.2-1992);
AT&T SVID Issue 2;
X/Open CAE Specification, Commands and Utilities, Issue 4, 1992.

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003