expr.1 (3783B)
1 .\" $OpenBSD: src/bin/expr/expr.1,v 1.22 2014/02/23 18:13:27 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: September 3 2010 $ 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 17 The 18 .Nm 19 utility evaluates 20 .Ar expression 21 and writes the result on standard output. 22 All operators are separate arguments to the 23 .Nm 24 utility. 25 Characters special to the command interpreter must be escaped. 26 .Pp 27 Operators are listed below in order of increasing precedence. 28 Operators with equal precedence are grouped within { } symbols. 29 .Bl -tag -width indent 30 .It Ar expr1 | expr2 31 Returns the evaluation of 32 .Ar expr1 33 if it is neither an empty string nor zero; 34 otherwise, returns the evaluation of 35 .Ar expr2 . 36 .It Ar expr1 Li & Ar expr2 37 Returns the evaluation of 38 .Ar expr1 39 if neither expression evaluates to an empty string or zero; 40 otherwise, returns zero. 41 .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2 42 Returns the results of integer comparison if both arguments are integers; 43 otherwise, returns the results of string comparison using the locale-specific 44 collation sequence. 45 The result of each comparison is 1 if the specified relation is true, 46 or 0 if the relation is false. 47 .It Ar expr1 Li "{+, -}" Ar expr2 48 Returns the results of addition or subtraction of integer-valued arguments. 49 .It Ar expr1 Li "{*, /, %}" Ar expr2 50 Returns the results of multiplication, integer division, or remainder of 51 integer-valued arguments. 52 .It Ar expr1 Li : Ar expr2 53 The 54 .Ql \&: 55 operator matches 56 .Ar expr1 57 against 58 .Ar expr2 , 59 which must be a basic regular expression. 60 The regular expression is anchored 61 to the beginning of the string with an implicit 62 .Ql ^ . 63 .Pp 64 If the match succeeds and the pattern contains at least one regular 65 expression subexpression 66 .Dq "\e(...\e)" , 67 the string corresponding to 68 .Dq "\e1" 69 is returned; 70 otherwise, the matching operator returns the number of characters matched. 71 If the match fails and the pattern contains a regular expression subexpression 72 the null string is returned; 73 otherwise, returns 0. 74 .Pp 75 Note: the empty string cannot be matched using 76 .Bd -literal -offset indent 77 expr '' : '$' 78 .Ed 79 .Pp 80 This is because the returned number of matched characters 81 .Pq zero 82 is indistinguishable from a failed match, so 83 .Nm 84 returns failure 85 .Pq 0 . 86 To match the empty string, use a structure such as: 87 .Bd -literal -offset indent 88 expr X'' : 'X$' 89 .Ed 90 .El 91 .Pp 92 Parentheses are used for grouping in the usual manner. 93 .Sh EXIT STATUS 94 The 95 .Nm 96 utility exits with one of the following values: 97 .Pp 98 .Bl -tag -width Ds -offset indent -compact 99 .It 0 100 The expression is neither an empty string nor 0. 101 .It 1 102 The expression is an empty string or 0. 103 .It 2 104 The expression is invalid. 105 .It \*(Gt2 106 An error occurred (such as memory allocation failure). 107 .El 108 .Sh EXAMPLES 109 Add 1 to the variable 110 .Va a : 111 .Bd -literal -offset indent 112 $ a=`expr $a + 1` 113 .Ed 114 .Pp 115 Return the filename portion of a pathname stored 116 in variable 117 .Va a . 118 The 119 .Ql // 120 characters act to eliminate ambiguity with the division operator: 121 .Bd -literal -offset indent 122 $ expr "//$a" \&: '.*/\e(.*\e)' 123 .Ed 124 .Pp 125 Return 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 134 The 135 .Nm 136 utility is compliant with the 137 .St -p1003.1-2008 138 specification. 139 .Sh HISTORY 140 The 141 .Nm 142 utility first appeared in the Programmer's Workbench (PWB/UNIX) 143 and has supported regular expressions since 144 .At v7 . 145 It was rewritten from scratch for 146 .Bx 386 0.1 147 and again for 148 .Nx 1.1 . 149 .Sh AUTHORS 150 .An -nosplit 151 The first free version was written by 152 .An Pace Willisson 153 in 1992. 154 This version was written by 155 .An John T. Conklin 156 in 1994.