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