1.\" $NetBSD: expr.1,v 1.10 1997/10/20 08:51:56 enami Exp $ 2.\" 3.\" Written by J.T. Conklin <jtc@netbsd.org>. 4.\" Public domain. 5.\" 6.Dd July 3, 1993 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.El 73.Pp 74Parentheses are used for grouping in the usual manner. 75.Sh EXAMPLES 76.Bl -enum 77.It 78The following example adds one to the variable a. 79.Dl a=`expr $a + 1` 80.It 81The following example returns the filename portion of a pathname stored 82in variable a. The // characters act to eliminate ambiguity with the 83division operator. 84.Dl expr "//$a" Li : '.*/\e(.*\e)' 85.It 86The following example returns the number of characters in variable a. 87.Dl expr $a Li : '.*' 88.El 89.Sh DIAGNOSTICS 90The 91.Nm 92utility exits with one of the following values: 93.Bl -tag -width Ds -compact 94.It 0 95the expression is neither an empty string nor 0. 96.It 1 97the expression is an empty string or 0. 98.It 2 99the expression is invalid. 100.El 101.Sh STANDARDS 102The 103.Nm 104utility conforms to 105.St -p1003.2 . 106