1.\" $OpenBSD: expr.1,v 1.13 2003/03/12 20:12:35 deraadt Exp $ 2.\" $NetBSD: expr.1,v 1.9 1995/04/28 23:27:13 jtc Exp $ 3.\" 4.\" Written by J.T. Conklin <jtc@netbsd.org>. 5.\" Public domain. 6.\" 7.Dd July 3, 1993 8.Dt EXPR 1 9.Os 10.Sh NAME 11.Nm expr 12.Nd evaluate expression 13.Sh SYNOPSIS 14.Nm expr 15.Ar expression 16.Sh DESCRIPTION 17The 18.Nm 19utility evaluates 20.Ar expression 21and writes the result on standard output. 22All operators are separate arguments to the 23.Nm 24utility. 25Characters special to the command interpreter must be escaped. 26.Pp 27Operators are listed below in order of increasing precedence. 28Operators with equal precedence are grouped within { } symbols. 29.Bl -tag -width indent 30.It Ar expr1 Li | Ar expr2 31Returns the evaluation of 32.Ar expr1 33if it is neither an empty string nor zero; 34otherwise, returns the evaluation of 35.Ar expr2 . 36.It Ar expr1 Li & Ar expr2 37Returns the evaluation of 38.Ar expr1 39if neither expression evaluates to an empty string or zero; 40otherwise, returns zero. 41.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2 42Returns the results of integer comparison if both arguments are integers; 43otherwise, returns the results of string comparison using the locale-specific 44collation sequence. 45The result of each comparison is 1 if the specified relation is true, 46or 0 if the relation is false. 47.It Ar expr1 Li "{+, -}" Ar expr2 48Returns the results of addition or subtraction of integer-valued arguments. 49.It Ar expr1 Li "{*, /, %}" Ar expr2 50Returns the results of multiplication, integer division, or remainder of 51integer-valued arguments. 52.It Ar expr1 Li : Ar expr2 53The 54.Ql \&: 55operator matches 56.Ar expr1 57against 58.Ar expr2 , 59which must be a regular expression. 60The regular expression is anchored 61to the beginning of the string with an implicit 62.Ql ^ . 63.Pp 64If the match succeeds and the pattern contains at least one regular 65expression subexpression 66.Dq "\e(...\e)" , 67the string corresponding to 68.Dq "\e1" 69is returned; 70otherwise, the matching operator returns the number of characters matched. 71If the match fails and the pattern contains a regular expression subexpression 72the null string is returned; 73otherwise, returns 0. 74.El 75.Pp 76Parentheses are used for grouping in the usual manner. 77.Sh EXAMPLES 78.Li $ a=`expr $a + 1` 79.Pp 80Add 1 to the variable 81.Va a . 82.Pp 83.Li $ expr "//$a" \&: '.*/\e(.*\e)' 84.Pp 85Return the filename portion of a pathname stored 86in variable 87.Va a . 88The 89.Ql // 90characters act to eliminate ambiguity with the division operator. 91.Pp 92.Li $ expr $a \&: '.*' 93.Pp 94Return the number of characters in variable 95.Va a . 96.Sh DIAGNOSTICS 97The 98.Nm 99utility exits with one of the following values: 100.Pp 101.Bl -tag -width Ds -compact 102.It 0 103The expression is neither an empty string nor 0. 104.It 1 105The expression is an empty string or 0. 106.It 2 107The expression is invalid. 108.It >2 109An error occurred (such as memory allocation failure). 110.El 111.Sh SEE ALSO 112.Xr test 1 113.Sh STANDARDS 114The 115.Nm 116utility conforms to 117.St -p1003.2 . 118