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