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.
31 .\" $FreeBSD: src/bin/expr/expr.1,v 1.31 2011/07/09 12:05:53 se Exp $
38 .Nd evaluate expression
47 and writes the result on standard output.
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.
55 Arithmetic operations are performed using signed integer math with a
56 range according to the C
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.
63 Operators are listed below in order of increasing precedence; all
65 Operators with equal precedence are grouped within symbols
69 .Bl -tag -width indent
70 .It Ar expr1 Li | Ar expr2
71 Return the evaluation of
73 if it is neither an empty string nor zero;
74 otherwise, returns the evaluation of
76 if it is not an empty string;
77 otherwise, returns zero.
78 .It Ar expr1 Li & Ar expr2
79 Return the evaluation of
81 if neither expression evaluates to an empty string or zero;
82 otherwise, returns zero.
83 .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
84 Return the results of integer comparison if both arguments are integers;
85 otherwise, returns the results of string comparison using the locale-specific
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
90 Return the results of addition or subtraction of integer-valued arguments.
91 .It Ar expr1 Li "{*, /, %}" Ar expr2
92 Return the results of multiplication, integer division, or remainder of integer-valued arguments.
93 .It Ar expr1 Li : Ar expr2
100 which must be a basic regular expression.
101 The regular expression is anchored
102 to the beginning of the string with an implicit
105 If the match succeeds and the pattern contains at least one regular
106 expression subexpression
108 the string corresponding to
111 otherwise the matching operator returns the number of characters matched.
112 If the match fails and the pattern contains a regular expression subexpression
113 the null string is returned;
117 Parentheses are used for grouping in the usual manner.
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
125 See the examples below for a work-around.
129 command in general is historic and inconvenient.
130 New applications are advised to use shell arithmetic rather than
135 utility exits with one of the following values:
136 .Bl -tag -width indent -compact
138 the expression is neither an empty string nor 0.
140 the expression is an empty string or 0.
142 the expression is invalid.
147 The following example (in
149 syntax) adds one to the variable
151 .Dl "a=$(expr $a + 1)"
153 This will fail if the value of
155 is a negative number.
156 To protect negative values of
158 from being interpreted as options to the
160 command, one might rearrange the expression:
161 .Dl "a=$(expr 1 + $a)"
163 More generally, parenthesize possibly-negative values:
164 .Dl "a=$(expr \e( $a \e) + 1)"
166 With shell arithmetic, no escaping is required:
169 This example prints the filename portion of a pathname stored
174 might represent the path
176 it is necessary to prevent it from being interpreted as the division operator.
179 characters resolve this ambiguity.
180 .Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
186 expands to the same value.
189 The following examples output the number of characters in variable
193 might begin with a hyphen, it is necessary to prevent it from being
194 interpreted as an option to
198 might be interpreted as an operator.
201 To deal with all of this, a complicated command
203 .Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
207 syntax, this can be done much more easily:
209 expands to the required number.
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.
227 standard, the use of string arguments
233 produces undefined results. In this version of
235 these arguments are treated just as their respective string values.