1.\" $OpenBSD: expr.1,v 1.23 2015/01/16 15:30:10 schwarze 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 $Mdocdate: January 16 2015 $ 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 | 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 basic 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.Pp 75Note: the empty string cannot be matched using 76.Bd -literal -offset indent 77expr '' : '$' 78.Ed 79.Pp 80This is because the returned number of matched characters 81.Pq zero 82is indistinguishable from a failed match, so 83.Nm 84returns failure 85.Pq 0 . 86To match the empty string, use a structure such as: 87.Bd -literal -offset indent 88expr X'' : 'X$' 89.Ed 90.El 91.Pp 92Parentheses are used for grouping in the usual manner. 93.Sh EXIT STATUS 94The 95.Nm 96utility exits with one of the following values: 97.Pp 98.Bl -tag -width Ds -offset indent -compact 99.It 0 100The expression is neither an empty string nor 0. 101.It 1 102The expression is an empty string or 0. 103.It 2 104The expression is invalid. 105.It \*(Gt2 106An error occurred (such as memory allocation failure). 107.El 108.Sh EXAMPLES 109Add 1 to the variable 110.Va a : 111.Bd -literal -offset indent 112$ a=`expr $a + 1` 113.Ed 114.Pp 115Return the filename portion of a pathname stored 116in variable 117.Va a . 118The 119.Ql // 120characters act to eliminate ambiguity with the division operator: 121.Bd -literal -offset indent 122$ expr "//$a" \&: '.*/\e(.*\e)' 123.Ed 124.Pp 125Return the number of characters in variable 126.Va a : 127.Bd -literal -offset indent 128$ expr $a \&: '.*' 129.Ed 130.Sh SEE ALSO 131.Xr test 1 , 132.Xr re_format 7 133.Sh STANDARDS 134The 135.Nm 136utility is compliant with the 137.St -p1003.1-2008 138specification. 139.Sh HISTORY 140The 141.Nm 142utility first appeared in the Programmer's Workbench (PWB/UNIX) 143and has supported regular expressions since 144.At v7 . 145It was rewritten from scratch for 146.Bx 386 0.1 147and again for 148.Nx 1.1 . 149.Sh AUTHORS 150.An -nosplit 151The first free version was written by 152.An Pace Willisson 153in 1992. 154This version was written by 155.An John T. Conklin 156in 1994. 157