xref: /freebsd/bin/expr/expr.1 (revision 4c8945a06b01a5c8122cdeb402af36bb46a06acc)
1.\" -*- nroff -*-
2.\"-
3.\" Copyright (c) 1993 Winning Strategies, Inc.
4.\" All rights reserved.
5.\"
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
19.\"
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$
32.\"
33.Dd September 9, 2010
34.Dt EXPR 1
35.Os
36.Sh NAME
37.Nm expr
38.Nd evaluate expression
39.Sh SYNOPSIS
40.Nm
41.Op Fl e
42.Ar expression
43.Sh DESCRIPTION
44The
45.Nm
46utility evaluates
47.Ar expression
48and writes the result on standard output.
49.Pp
50All operators and operands must be passed as separate arguments.
51Several of the operators have special meaning to command interpreters
52and must therefore be quoted appropriately.
53All integer operands are interpreted in base 10.
54.Pp
55Arithmetic operations are performed using signed integer math.
56If the
57.Fl e
58flag is specified, arithmetic uses the C
59.Vt intmax_t
60data type (the largest integral type available), and
61.Nm
62will detect arithmetic overflow and return an error indication.
63If a numeric operand is specified which is so large as to overflow
64conversion to an integer, it is parsed as a string instead.
65If
66.Fl e
67is not specified, arithmetic operations and parsing of integer
68arguments will overflow silently according to the rules of the C
69standard, using the
70.Vt long
71data type.
72.Pp
73Operators are listed below in order of increasing precedence; all
74are left-associative.
75Operators with equal precedence are grouped within symbols
76.Ql {
77and
78.Ql } .
79.Bl -tag -width indent
80.It Ar expr1 Li | Ar expr2
81Return the evaluation of
82.Ar expr1
83if it is neither an empty string nor zero;
84otherwise, returns the evaluation of
85.Ar expr2 .
86.It Ar expr1 Li & Ar expr2
87Return the evaluation of
88.Ar expr1
89if neither expression evaluates to an empty string or zero;
90otherwise, returns zero.
91.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
92Return the results of integer comparison if both arguments are integers;
93otherwise, returns the results of string comparison using the locale-specific
94collation sequence.
95The result of each comparison is 1 if the specified relation is true,
96or 0 if the relation is false.
97.It Ar expr1 Li "{+, -}" Ar expr2
98Return the results of addition or subtraction of integer-valued arguments.
99.It Ar expr1 Li "{*, /, %}" Ar expr2
100Return the results of multiplication, integer division, or remainder of integer-valued arguments.
101.It Ar expr1 Li : Ar expr2
102The
103.Dq Li \&:
104operator matches
105.Ar expr1
106against
107.Ar expr2 ,
108which must be a basic regular expression.
109The regular expression is anchored
110to the beginning of the string with an implicit
111.Dq Li ^ .
112.Pp
113If the match succeeds and the pattern contains at least one regular
114expression subexpression
115.Dq Li "\e(...\e)" ,
116the string corresponding to
117.Dq Li \e1
118is returned;
119otherwise the matching operator returns the number of characters matched.
120If the match fails and the pattern contains a regular expression subexpression
121the null string is returned;
122otherwise 0.
123.El
124.Pp
125Parentheses are used for grouping in the usual manner.
126.Pp
127The
128.Nm
129utility makes no lexical distinction between arguments which may be
130operators and arguments which may be operands.
131An operand which is lexically identical to an operator will be considered a
132syntax error.
133See the examples below for a work-around.
134.Pp
135The syntax of the
136.Nm
137command in general is historic and inconvenient.
138New applications are advised to use shell arithmetic rather than
139.Nm .
140.Ss Compatibility with previous implementations
141Unless
142.Fx
1434.x
144compatibility is enabled, this version of
145.Nm
146adheres to the
147.Tn POSIX
148Utility Syntax Guidelines, which require that a leading argument beginning
149with a minus sign be considered an option to the program.
150The standard
151.Fl Fl
152syntax may be used to prevent this interpretation.
153However, many historic implementations of
154.Nm ,
155including the one in previous versions of
156.Fx ,
157will not permit this syntax.
158See the examples below for portable ways to guarantee the correct
159interpretation.
160The
161.Xr check_utility_compat 3
162function (with a
163.Fa utility
164argument of
165.Dq Li expr )
166is used to determine whether compatibility mode should be enabled.
167This feature is intended for use as a transition and debugging aid, when
168.Nm
169is used in complex scripts which cannot easily be recast to avoid the
170non-portable usage.
171Enabling compatibility mode
172also implicitly enables the
173.Fl e
174option, since this matches the historic behavior of
175.Nm
176in
177.Fx .
178For historical reasons, defining the environment variable
179.Ev EXPR_COMPAT
180also enables compatibility mode.
181.Sh ENVIRONMENT
182.Bl -tag -width ".Ev EXPR_COMPAT"
183.It Ev EXPR_COMPAT
184If set, enables compatibility mode.
185.El
186.Sh EXIT STATUS
187The
188.Nm
189utility exits with one of the following values:
190.Bl -tag -width indent -compact
191.It 0
192the expression is neither an empty string nor 0.
193.It 1
194the expression is an empty string or 0.
195.It 2
196the expression is invalid.
197.El
198.Sh EXAMPLES
199.Bl -bullet
200.It
201The following example (in
202.Xr sh 1
203syntax) adds one to the variable
204.Va a :
205.Dl "a=$(expr $a + 1)"
206.It
207This will fail if the value of
208.Va a
209is a negative number.
210To protect negative values of
211.Va a
212from being interpreted as options to the
213.Nm
214command, one might rearrange the expression:
215.Dl "a=$(expr 1 + $a)"
216.It
217More generally, parenthesize possibly-negative values:
218.Dl "a=$(expr \e( $a \e) + 1)"
219.It
220With shell arithmetic, no escaping is required:
221.Dl "a=$((a + 1))"
222.It
223This example prints the filename portion of a pathname stored
224in variable
225.Va a .
226Since
227.Va a
228might represent the path
229.Pa / ,
230it is necessary to prevent it from being interpreted as the division operator.
231The
232.Li //
233characters resolve this ambiguity.
234.Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
235.It
236With modern
237.Xr sh 1
238syntax,
239.Dl "\*q${a##*/}\*q"
240expands to the same value.
241.El
242.Pp
243The following examples output the number of characters in variable
244.Va a .
245Again, if
246.Va a
247might begin with a hyphen, it is necessary to prevent it from being
248interpreted as an option to
249.Nm ,
250and
251.Va a
252might be interpreted as an operator.
253.Bl -bullet
254.It
255To deal with all of this, a complicated command
256is required:
257.Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
258.It
259With modern
260.Xr sh 1
261syntax, this can be done much more easily:
262.Dl "${#a}"
263expands to the required number.
264.El
265.Sh SEE ALSO
266.Xr sh 1 ,
267.Xr test 1 ,
268.Xr check_utility_compat 3
269.Sh STANDARDS
270The
271.Nm
272utility conforms to
273.St -p1003.1-2001 ,
274provided that compatibility mode is not enabled.
275The
276.Fl e
277flag is an extension.
278