1.\" $NetBSD: expr.1,v 1.21 2002/09/25 15:18:39 wiz Exp $ 2.\" 3.\" Written by J.T. Conklin <jtc@netbsd.org>. 4.\" Public domain. 5.\" 6.Dd September 18, 2000 7.Dt EXPR 1 8.Os 9.Sh NAME 10.Nm expr 11.Nd evaluate expression 12.Sh SYNOPSIS 13.Nm 14.Ar expression 15.Sh DESCRIPTION 16The 17.Nm 18utility evaluates 19.Ar expression 20and writes the result on standard output. 21.Pp 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 \*[Am] 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 "{=, \*[Gt], \*[Ge], \*[Lt], \*[Le], !=}" 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 integer-valued arguments. 51.It Ar expr1 Li : Ar expr2 52The 53.Dq \&: 54operator matches 55.Ar expr1 56against 57.Ar expr2 , 58which must be a regular expression. 59The regular expression is anchored 60to the beginning of the string with an implicit 61.Dq ^ . 62.Pp 63If the match succeeds and the pattern contains at least one regular 64expression subexpression 65.Dq "\e(...\e)" , 66the string corresponding to 67.Dq "\e1" 68is returned; 69otherwise the matching operator returns the number of characters matched. 70If the match fails and the pattern contains a regular expression subexpression 71the null string is returned; 72otherwise 0. 73.It Ar "( " expr Li " )" 74Parentheses are used for grouping in the usual manner. 75.El 76.Pp 77Operator precedence (from highest to lowest): 78.Bl -enum -compact -offset indent 79.It 80parentheses 81.It 82.Dq \&: 83.It 84.Dq "*" , 85.Dq "/" , 86and 87.Dq "%" 88.It 89.Dq "+" 90and 91.Dq "-" 92.It 93compare operators 94.It 95.Dq \*[Am] 96.It 97.Dq \Z'\*[tty-rn]'| 98.El 99.Sh EXIT STATUS 100The 101.Nm 102utility exits with one of the following values: 103.Bl -tag -width Ds -compact 104.It 0 105the expression is neither an empty string nor 0. 106.It 1 107the expression is an empty string or 0. 108.It 2 109the expression is invalid. 110.It \*[Gt]2 111an error occurred (such as memory allocation failure). 112.El 113.Sh EXAMPLES 114.Bl -enum 115.It 116The following example adds one to the variable a. 117.Dl a=`expr $a + 1` 118.It 119The following example returns zero, due to deduction having higher precendence 120than '\*[Am]' operator. 121.Dl expr 1 '\*[Am]' 1 - 1 122.It 123The following example returns the filename portion of a pathname stored 124in variable a. 125.Dl expr "/$a" Li : '.*/\e(.*\e)' 126.It 127The following example returns the number of characters in variable a. 128.Dl expr $a Li : '.*' 129.El 130.Sh STANDARDS 131The 132.Nm 133utility conforms to 134.St -p1003.2 . 135.Sh AUTHORS 136Original implementation was written by 137.An J.T. Conklin Aq jtc@netbsd.org . 138It was rewritten for 139.Nx 1.6 140by 141.An Jaromir Dolecek Aq jdolecek@netbsd.org . 142.Sh COMPATIBILITY 143This implementation of 144.Nm 145internally uses 64 bit represenation of integers and checks for 146over- and underflows. 147It also treats / (division mark) and 148option '--' correctly depending upon context. 149.Pp 150.Nm 151on other systems (including 152.Nx 153up to and including 154.Nx 1.5 ) 155might be not so graceful. 156Arithmetic results might be arbitrarily 157limited on such systems, most commonly to 32 bit quantities. 158This means such 159.Nm 160can only process values between -2147483648 and +2147483647. 161.Pp 162On other systems, 163.Nm 164might also not work correctly for regular expressions where 165either side contains single forward slash, like this: 166.Bd -literal -offset indent 167expr / : '.*/\e(.*\e)' 168.Ed 169.Pp 170If this is the case, you might use // (double forward slash) 171to avoid abiquity with the division operator: 172.Bd -literal -offset indent 173expr "//$a" : '.*/\e(.*\e)' 174.Ed 175.Pp 176According to 177.St -p1003.2 , 178.Nm 179has to recognize special option '--', treat it as an end of command 180line options and ignore it. 181Some 182.Nm 183implementations don't recognize it at all, others 184might ignore it even in cases where doing so results in syntax 185error. 186There should be same result for both following examples, 187but it might not always be: 188.Bl -enum -compact -offset indent 189.It 190expr -- : . 191.It 192expr -- -- : . 193.El 194Althrough 195.Nx 196.Nm 197handles both cases correctly, you should not depend on this behaviour 198for portability reasons and avoid passing bare '--' as first 199argument. 200