3 .\" Copyright (c) 1993 Winning Strategies, Inc.
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
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
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.
38 .Nd evaluate expression
48 and writes the result on standard output.
50 All operators and operands must be passed as separate arguments.
51 Several of the operators have special meaning to command interpreters
52 and must therefore be quoted appropriately.
53 All integer operands are interpreted in base 10.
55 Arithmetic operations are performed using signed integer math.
58 flag is specified, arithmetic uses the C
60 data type (the largest integral type available), and
62 will detect arithmetic overflow and return an error indication.
63 If a numeric operand is specified which is so large as to overflow
64 conversion to an integer, it is parsed as a string instead.
67 is not specified, arithmetic operations and parsing of integer
68 arguments will overflow silently according to the rules of the C
73 Operators are listed below in order of increasing precedence; all
75 Operators with equal precedence are grouped within symbols
79 .Bl -tag -width indent
80 .It Ar expr1 Li | Ar expr2
81 Return the evaluation of
83 if it is neither an empty string nor zero;
84 otherwise, returns the evaluation of
86 .It Ar expr1 Li & Ar expr2
87 Return the evaluation of
89 if neither expression evaluates to an empty string or zero;
90 otherwise, returns zero.
91 .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
92 Return the results of integer comparison if both arguments are integers;
93 otherwise, returns the results of string comparison using the locale-specific
95 The result of each comparison is 1 if the specified relation is true,
96 or 0 if the relation is false.
97 .It Ar expr1 Li "{+, -}" Ar expr2
98 Return the results of addition or subtraction of integer-valued arguments.
99 .It Ar expr1 Li "{*, /, %}" Ar expr2
100 Return the results of multiplication, integer division, or remainder of integer-valued arguments.
101 .It Ar expr1 Li : Ar expr2
108 which must be a basic regular expression.
109 The regular expression is anchored
110 to the beginning of the string with an implicit
113 If the match succeeds and the pattern contains at least one regular
114 expression subexpression
116 the string corresponding to
119 otherwise the matching operator returns the number of characters matched.
120 If the match fails and the pattern contains a regular expression subexpression
121 the null string is returned;
125 Parentheses are used for grouping in the usual manner.
129 utility makes no lexical distinction between arguments which may be
130 operators and arguments which may be operands.
131 An operand which is lexically identical to an operator will be considered a
133 See the examples below for a work-around.
137 command in general is historic and inconvenient.
138 New applications are advised to use shell arithmetic rather than
140 .Ss Compatibility with previous implementations
144 compatibility is enabled, this version of
148 Utility Syntax Guidelines, which require that a leading argument beginning
149 with a minus sign be considered an option to the program.
152 syntax may be used to prevent this interpretation.
153 However, many historic implementations of
155 including the one in previous versions of
157 will not permit this syntax.
158 See the examples below for portable ways to guarantee the correct
161 .Xr check_utility_compat 3
166 is used to determine whether compatibility mode should be enabled.
167 This feature is intended for use as a transition and debugging aid, when
169 is used in complex scripts which cannot easily be recast to avoid the
171 Enabling compatibility mode
172 also implicitly enables the
174 option, since this matches the historic behavior of
178 For historical reasons, defining the environment variable
180 also enables compatibility mode.
182 .Bl -tag -width ".Ev EXPR_COMPAT"
184 If set, enables compatibility mode.
189 utility exits with one of the following values:
190 .Bl -tag -width indent -compact
192 the expression is neither an empty string nor 0.
194 the expression is an empty string or 0.
196 the expression is invalid.
201 The following example (in
203 syntax) adds one to the variable
205 .Dl "a=$(expr $a + 1)"
207 This will fail if the value of
209 is a negative number.
210 To protect negative values of
212 from being interpreted as options to the
214 command, one might rearrange the expression:
215 .Dl "a=$(expr 1 + $a)"
217 More generally, parenthesize possibly-negative values:
218 .Dl "a=$(expr \e( $a \e) + 1)"
220 This example prints the filename portion of a pathname stored
225 might represent the path
227 it is necessary to prevent it from being interpreted as the division operator.
230 characters resolve this ambiguity.
231 .Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
234 The following examples output the number of characters in variable
238 might begin with a hyphen, it is necessary to prevent it from being
239 interpreted as an option to
248 .Dl "expr -- \*q$a\*q \&: \*q.*\*q"
250 For portability to older systems, however, a more complicated command
252 .Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
257 .Xr check_utility_compat 3
263 provided that compatibility mode is not enabled.
266 flag is an extension.