]>
Commit | Line | Data |
---|---|---|
1e9ba8f2 A |
1 | .\" -*- nroff -*- |
2 | .\"- | |
3 | .\" Copyright (c) 1993 Winning Strategies, Inc. | |
4 | .\" All rights reserved. | |
44bd5ea7 | 5 | .\" |
1e9ba8f2 A |
6 | .\" Redistribution and use in source and binary forms, with or without |
7 | .\" modification, are permitted provided that the following conditions | |
8 | .\" are met: | |
9 | .\" 1. Redistributions of source code must retain the above copyright | |
10 | .\" notice, this list of conditions and the following disclaimer. | |
11 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
12 | .\" notice, this list of conditions and the following disclaimer in the | |
13 | .\" documentation and/or other materials provided with the distribution. | |
14 | .\" 3. All advertising materials mentioning features or use of this software | |
15 | .\" must display the following acknowledgement: | |
16 | .\" This product includes software developed by Winning Strategies, Inc. | |
17 | .\" 4. The name of the author may not be used to endorse or promote products | |
18 | .\" derived from this software without specific prior written permission | |
44bd5ea7 | 19 | .\" |
1e9ba8f2 A |
20 | .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
21 | .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
22 | .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
23 | .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, | |
24 | .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
25 | .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | |
26 | .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | |
27 | .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
28 | .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | |
29 | .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
30 | .\" | |
31 | .\" $FreeBSD: src/bin/expr/expr.1,v 1.31 2011/07/09 12:05:53 se Exp $ | |
32 | .\" | |
33 | .Dd September 9, 2010 | |
44bd5ea7 A |
34 | .Dt EXPR 1 |
35 | .Os | |
36 | .Sh NAME | |
37 | .Nm expr | |
38 | .Nd evaluate expression | |
39 | .Sh SYNOPSIS | |
40 | .Nm | |
41 | .Ar expression | |
42 | .Sh DESCRIPTION | |
43 | The | |
44 | .Nm | |
1e9ba8f2 | 45 | utility evaluates |
44bd5ea7 A |
46 | .Ar expression |
47 | and writes the result on standard output. | |
48 | .Pp | |
1e9ba8f2 A |
49 | All operators and operands must be passed as separate arguments. |
50 | Several of the operators have special meaning to command interpreters | |
51 | and must therefore be quoted appropriately. | |
52 | All integer operands are interpreted in base 10 and must consist of only | |
53 | an optional leading minus sign followed by one or more digits. | |
44bd5ea7 | 54 | .Pp |
1e9ba8f2 A |
55 | Arithmetic operations are performed using signed integer math with a |
56 | range according to the C | |
57 | .Vt intmax_t | |
58 | data type (the largest signed integral type available). | |
59 | All conversions and operations are checked for overflow. | |
60 | Overflow results in program termination with an error message on stdout | |
61 | and with an error status. | |
62 | .Pp | |
63 | Operators are listed below in order of increasing precedence; all | |
64 | are left-associative. | |
65 | Operators with equal precedence are grouped within symbols | |
66 | .Ql { | |
67 | and | |
68 | .Ql } . | |
44bd5ea7 A |
69 | .Bl -tag -width indent |
70 | .It Ar expr1 Li | Ar expr2 | |
1e9ba8f2 A |
71 | Return the evaluation of |
72 | .Ar expr1 | |
44bd5ea7 A |
73 | if it is neither an empty string nor zero; |
74 | otherwise, returns the evaluation of | |
1e9ba8f2 A |
75 | .Ar expr2 |
76 | if it is not an empty string; | |
77 | otherwise, returns zero. | |
44bd5ea7 | 78 | .It Ar expr1 Li & Ar expr2 |
1e9ba8f2 | 79 | Return the evaluation of |
44bd5ea7 A |
80 | .Ar expr1 |
81 | if neither expression evaluates to an empty string or zero; | |
82 | otherwise, returns zero. | |
83 | .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2 | |
1e9ba8f2 | 84 | Return the results of integer comparison if both arguments are integers; |
44bd5ea7 A |
85 | otherwise, returns the results of string comparison using the locale-specific |
86 | collation sequence. | |
87 | The result of each comparison is 1 if the specified relation is true, | |
88 | or 0 if the relation is false. | |
89 | .It Ar expr1 Li "{+, -}" Ar expr2 | |
1e9ba8f2 | 90 | Return the results of addition or subtraction of integer-valued arguments. |
44bd5ea7 | 91 | .It Ar expr1 Li "{*, /, %}" Ar expr2 |
1e9ba8f2 | 92 | Return the results of multiplication, integer division, or remainder of integer-valued arguments. |
44bd5ea7 | 93 | .It Ar expr1 Li : Ar expr2 |
1e9ba8f2 A |
94 | The |
95 | .Dq Li \&: | |
96 | operator matches | |
97 | .Ar expr1 | |
98 | against | |
44bd5ea7 | 99 | .Ar expr2 , |
1e9ba8f2 A |
100 | which must be a basic regular expression. |
101 | The regular expression is anchored | |
102 | to the beginning of the string with an implicit | |
103 | .Dq Li ^ . | |
44bd5ea7 A |
104 | .Pp |
105 | If the match succeeds and the pattern contains at least one regular | |
1e9ba8f2 A |
106 | expression subexpression |
107 | .Dq Li "\e(...\e)" , | |
108 | the string corresponding to | |
109 | .Dq Li \e1 | |
44bd5ea7 | 110 | is returned; |
1e9ba8f2 | 111 | otherwise the matching operator returns the number of characters matched. |
44bd5ea7 A |
112 | If the match fails and the pattern contains a regular expression subexpression |
113 | the null string is returned; | |
114 | otherwise 0. | |
115 | .El | |
116 | .Pp | |
117 | Parentheses are used for grouping in the usual manner. | |
1e9ba8f2 A |
118 | .Pp |
119 | The | |
120 | .Nm | |
121 | utility makes no lexical distinction between arguments which may be | |
122 | operators and arguments which may be operands. | |
123 | An operand which is lexically identical to an operator will be considered a | |
124 | syntax error. | |
125 | See the examples below for a work-around. | |
126 | .Pp | |
127 | The syntax of the | |
128 | .Nm | |
129 | command in general is historic and inconvenient. | |
130 | New applications are advised to use shell arithmetic rather than | |
131 | .Nm . | |
132 | .Sh EXIT STATUS | |
44bd5ea7 A |
133 | The |
134 | .Nm | |
135 | utility exits with one of the following values: | |
1e9ba8f2 | 136 | .Bl -tag -width indent -compact |
44bd5ea7 A |
137 | .It 0 |
138 | the expression is neither an empty string nor 0. | |
139 | .It 1 | |
140 | the expression is an empty string or 0. | |
141 | .It 2 | |
142 | the expression is invalid. | |
143 | .El | |
1e9ba8f2 A |
144 | .Sh EXAMPLES |
145 | .Bl -bullet | |
146 | .It | |
147 | The following example (in | |
148 | .Xr sh 1 | |
149 | syntax) adds one to the variable | |
150 | .Va a : | |
151 | .Dl "a=$(expr $a + 1)" | |
152 | .It | |
153 | This will fail if the value of | |
154 | .Va a | |
155 | is a negative number. | |
156 | To protect negative values of | |
157 | .Va a | |
158 | from being interpreted as options to the | |
159 | .Nm | |
160 | command, one might rearrange the expression: | |
161 | .Dl "a=$(expr 1 + $a)" | |
162 | .It | |
163 | More generally, parenthesize possibly-negative values: | |
164 | .Dl "a=$(expr \e( $a \e) + 1)" | |
165 | .It | |
166 | With shell arithmetic, no escaping is required: | |
167 | .Dl "a=$((a + 1))" | |
168 | .It | |
169 | This example prints the filename portion of a pathname stored | |
170 | in variable | |
171 | .Va a . | |
172 | Since | |
173 | .Va a | |
174 | might represent the path | |
175 | .Pa / , | |
176 | it is necessary to prevent it from being interpreted as the division operator. | |
177 | The | |
178 | .Li // | |
179 | characters resolve this ambiguity. | |
180 | .Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'" | |
181 | .It | |
182 | With modern | |
183 | .Xr sh 1 | |
184 | syntax, | |
185 | .Dl "\*q${a##*/}\*q" | |
186 | expands to the same value. | |
187 | .El | |
188 | .Pp | |
189 | The following examples output the number of characters in variable | |
190 | .Va a . | |
191 | Again, if | |
192 | .Va a | |
193 | might begin with a hyphen, it is necessary to prevent it from being | |
194 | interpreted as an option to | |
195 | .Nm , | |
196 | and | |
197 | .Va a | |
198 | might be interpreted as an operator. | |
199 | .Bl -bullet | |
200 | .It | |
201 | To deal with all of this, a complicated command | |
202 | is required: | |
203 | .Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1" | |
204 | .It | |
205 | With modern | |
206 | .Xr sh 1 | |
207 | syntax, this can be done much more easily: | |
208 | .Dl "${#a}" | |
209 | expands to the required number. | |
210 | .El | |
211 | .Sh SEE ALSO | |
212 | .Xr sh 1 , | |
213 | .Xr test 1 | |
44bd5ea7 A |
214 | .Sh STANDARDS |
215 | The | |
216 | .Nm | |
217 | utility conforms to | |
1e9ba8f2 A |
218 | .St -p1003.1-2008 . |
219 | .Pp | |
220 | The extended arithmetic range and overflow checks do not conflict with | |
221 | POSIX's requirement that arithmetic be done using signed longs, since | |
222 | they only make a difference to the result in cases where using signed | |
223 | longs would give undefined behavior. | |
224 | .Pp | |
225 | According to the | |
226 | .Tn POSIX | |
227 | standard, the use of string arguments | |
228 | .Va length , | |
229 | .Va substr , | |
230 | .Va index , | |
231 | or | |
232 | .Va match | |
233 | produces undefined results. In this version of | |
234 | .Nm , | |
235 | these arguments are treated just as their respective string values. |