xref: /freebsd-14.2/bin/expr/expr.1 (revision 9ddb49cb)
1d8c21a3eSJ.T. Conklin.\" -*- nroff -*-
2*9ddb49cbSWarner Losh
3*9ddb49cbSWarner Losh.\"-
4c9fe00dcSJ.T. Conklin.\" Copyright (c) 1993 Winning Strategies, Inc.
5c9fe00dcSJ.T. Conklin.\" All rights reserved.
6c9fe00dcSJ.T. Conklin.\"
7c9fe00dcSJ.T. Conklin.\" Redistribution and use in source and binary forms, with or without
8c9fe00dcSJ.T. Conklin.\" modification, are permitted provided that the following conditions
9c9fe00dcSJ.T. Conklin.\" are met:
10c9fe00dcSJ.T. Conklin.\" 1. Redistributions of source code must retain the above copyright
11c9fe00dcSJ.T. Conklin.\"    notice, this list of conditions and the following disclaimer.
12c9fe00dcSJ.T. Conklin.\" 2. Redistributions in binary form must reproduce the above copyright
13c9fe00dcSJ.T. Conklin.\"    notice, this list of conditions and the following disclaimer in the
14c9fe00dcSJ.T. Conklin.\"    documentation and/or other materials provided with the distribution.
15c9fe00dcSJ.T. Conklin.\" 3. All advertising materials mentioning features or use of this software
16c9fe00dcSJ.T. Conklin.\"    must display the following acknowledgement:
17c9fe00dcSJ.T. Conklin.\"      This product includes software developed by Winning Strategies, Inc.
18c9fe00dcSJ.T. Conklin.\" 4. The name of the author may not be used to endorse or promote products
197d7ee86eSMike Pritchard.\"    derived from this software without specific prior written permission
20c9fe00dcSJ.T. Conklin.\"
21c9fe00dcSJ.T. Conklin.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22c9fe00dcSJ.T. Conklin.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23c9fe00dcSJ.T. Conklin.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24c9fe00dcSJ.T. Conklin.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25c9fe00dcSJ.T. Conklin.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26c9fe00dcSJ.T. Conklin.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27c9fe00dcSJ.T. Conklin.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28c9fe00dcSJ.T. Conklin.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29c9fe00dcSJ.T. Conklin.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30c9fe00dcSJ.T. Conklin.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31c9fe00dcSJ.T. Conklin.\"
322a456239SPeter Wemm.\" $FreeBSD$
33c9fe00dcSJ.T. Conklin.\"
345292d2aeSTim J. Robbins.Dd July 12, 2004
35c9fe00dcSJ.T. Conklin.Dt EXPR 1
36c9fe00dcSJ.T. Conklin.Os
37c9fe00dcSJ.T. Conklin.Sh NAME
38c9fe00dcSJ.T. Conklin.Nm expr
39c9fe00dcSJ.T. Conklin.Nd evaluate expression
40c9fe00dcSJ.T. Conklin.Sh SYNOPSIS
41be8b1497SRuslan Ermilov.Nm
421393277eSGarrett Wollman.Op Fl e
43c9fe00dcSJ.T. Conklin.Ar expression
44c9fe00dcSJ.T. Conklin.Sh DESCRIPTION
45c9fe00dcSJ.T. ConklinThe
4615638d89SPhilippe Charnier.Nm
47c9fe00dcSJ.T. Conklinutility evaluates
48c9fe00dcSJ.T. Conklin.Ar expression
49c9fe00dcSJ.T. Conklinand writes the result on standard output.
50c9fe00dcSJ.T. Conklin.Pp
51f07e4247SGarrett WollmanAll operators and operands must be passed as separate arguments.
52f07e4247SGarrett WollmanSeveral of the operators have special meaning to command interpreters
53f07e4247SGarrett Wollmanand must therefore be quoted appropriately.
541393277eSGarrett WollmanAll integer operands are interpreted in base 10.
55c9fe00dcSJ.T. Conklin.Pp
561393277eSGarrett WollmanArithmetic operations are performed using signed integer math.
571393277eSGarrett WollmanIf the
581393277eSGarrett Wollman.Fl e
591393277eSGarrett Wollmanflag is specified, arithmetic uses the C
609dfcbc3bSRuslan Ermilov.Vt intmax_t
611393277eSGarrett Wollmandata type (the largest integral type available), and
62f07e4247SGarrett Wollman.Nm
631393277eSGarrett Wollmanwill detect arithmetic overflow and return an error indication.
641393277eSGarrett WollmanIf a numeric operand is specified which is so large as to overflow
651393277eSGarrett Wollmanconversion to an integer, it is parsed as a string instead.
661393277eSGarrett WollmanIf
671393277eSGarrett Wollman.Fl e
681393277eSGarrett Wollmanis not specified, arithmetic operations and parsing of integer
691393277eSGarrett Wollmanarguments will overflow silently according to the rules of the C
7094a48596SGarrett Wollmanstandard, using the
719dfcbc3bSRuslan Ermilov.Vt long
721393277eSGarrett Wollmandata type.
73f07e4247SGarrett Wollman.Pp
74f07e4247SGarrett WollmanOperators are listed below in order of increasing precedence; all
75f07e4247SGarrett Wollmanare left-associative.
767d7ee86eSMike PritchardOperators with equal precedence are grouped within { } symbols.
77c9fe00dcSJ.T. Conklin.Bl -tag -width indent
78c9fe00dcSJ.T. Conklin.It Ar expr1 Li | Ar expr2
7915638d89SPhilippe CharnierReturn the evaluation of
80c9fe00dcSJ.T. Conklin.Ar expr1
81c9fe00dcSJ.T. Conklinif it is neither an empty string nor zero;
82c9fe00dcSJ.T. Conklinotherwise, returns the evaluation of
83c9fe00dcSJ.T. Conklin.Ar expr2 .
84c9fe00dcSJ.T. Conklin.It Ar expr1 Li & Ar expr2
8515638d89SPhilippe CharnierReturn the evaluation of
86c9fe00dcSJ.T. Conklin.Ar expr1
87c9fe00dcSJ.T. Conklinif neither expression evaluates to an empty string or zero;
88c9fe00dcSJ.T. Conklinotherwise, returns zero.
89c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
9015638d89SPhilippe CharnierReturn the results of integer comparison if both arguments are integers;
91d8c21a3eSJ.T. Conklinotherwise, returns the results of string comparison using the locale-specific
92d8c21a3eSJ.T. Conklincollation sequence.
93c9fe00dcSJ.T. ConklinThe result of each comparison is 1 if the specified relation is true,
94c9fe00dcSJ.T. Conklinor 0 if the relation is false.
95c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{+, -}" Ar expr2
9615638d89SPhilippe CharnierReturn the results of addition or subtraction of integer-valued arguments.
97c9fe00dcSJ.T. Conklin.It Ar expr1 Li "{*, /, %}" Ar expr2
9815638d89SPhilippe CharnierReturn the results of multiplication, integer division, or remainder of integer-valued arguments.
99c9fe00dcSJ.T. Conklin.It Ar expr1 Li : Ar expr2
100c9fe00dcSJ.T. ConklinThe
10112e720d7SRuslan Ermilov.Dq \&:
102c9fe00dcSJ.T. Conklinoperator matches
103c9fe00dcSJ.T. Conklin.Ar expr1
104c9fe00dcSJ.T. Conklinagainst
105c9fe00dcSJ.T. Conklin.Ar expr2 ,
106f07e4247SGarrett Wollmanwhich must be a basic regular expression.
107f07e4247SGarrett WollmanThe regular expression is anchored
1082caac73eSJordan K. Hubbardto the beginning of the string with an implicit
109d8c21a3eSJ.T. Conklin.Dq ^ .
110c9fe00dcSJ.T. Conklin.Pp
111d8c21a3eSJ.T. ConklinIf the match succeeds and the pattern contains at least one regular
112d8c21a3eSJ.T. Conklinexpression subexpression
113c9fe00dcSJ.T. Conklin.Dq "\e(...\e)" ,
114c9fe00dcSJ.T. Conklinthe string corresponding to
115c9fe00dcSJ.T. Conklin.Dq "\e1"
116c9fe00dcSJ.T. Conklinis returned;
117c9fe00dcSJ.T. Conklinotherwise the matching operator returns the number of characters matched.
118d8c21a3eSJ.T. ConklinIf the match fails and the pattern contains a regular expression subexpression
119d8c21a3eSJ.T. Conklinthe null string is returned;
120d8c21a3eSJ.T. Conklinotherwise 0.
121c9fe00dcSJ.T. Conklin.El
122c9fe00dcSJ.T. Conklin.Pp
123c9fe00dcSJ.T. ConklinParentheses are used for grouping in the usual manner.
124f07e4247SGarrett Wollman.Pp
125c9885518SGarrett WollmanThe
126c9885518SGarrett Wollman.Nm
127c9885518SGarrett Wollmanutility makes no lexical distinction between arguments which may be
128c9885518SGarrett Wollmanoperators and arguments which may be operands.
129c9885518SGarrett WollmanAn operand which is lexically identical to an operator will be considered a
130c9885518SGarrett Wollmansyntax error.
131c9885518SGarrett WollmanSee the examples below for a work-around.
132c9885518SGarrett Wollman.Pp
133c9885518SGarrett WollmanThe syntax of the
134c9885518SGarrett Wollman.Nm
135c9885518SGarrett Wollmancommand in general is historic and inconvenient.
136c9885518SGarrett WollmanNew applications are advised to use shell arithmetic rather than
137c9885518SGarrett Wollman.Nm .
138c9885518SGarrett Wollman.Ss Compatibility with previous implementations
139c9885518SGarrett WollmanUnless
14098d13c7eSRuslan Ermilov.Fx
14198d13c7eSRuslan Ermilov4.x
142c9885518SGarrett Wollmancompatibility is enabled, this version of
143f07e4247SGarrett Wollman.Nm
144f07e4247SGarrett Wollmanadheres to the
145e27480d6SRuslan Ermilov.Tn POSIX
146f07e4247SGarrett WollmanUtility Syntax Guidelines, which require that a leading argument beginning
147f07e4247SGarrett Wollmanwith a minus sign be considered an option to the program.
148f07e4247SGarrett WollmanThe standard
1499dfcbc3bSRuslan Ermilov.Fl Fl
150f07e4247SGarrett Wollmansyntax may be used to prevent this interpretation.
151f07e4247SGarrett WollmanHowever, many historic implementations of
152f07e4247SGarrett Wollman.Nm ,
153f07e4247SGarrett Wollmanincluding the one in previous versions of
154f07e4247SGarrett Wollman.Fx ,
155f07e4247SGarrett Wollmanwill not permit this syntax.
156f07e4247SGarrett WollmanSee the examples below for portable ways to guarantee the correct
157f07e4247SGarrett Wollmaninterpretation.
15894a48596SGarrett WollmanThe
159c9885518SGarrett Wollman.Xr check_utility_compat 3
160c9885518SGarrett Wollmanfunction (with a
161c9885518SGarrett Wollman.Fa utility
162c9885518SGarrett Wollmanargument of
163c9885518SGarrett Wollman.Dq Li expr )
164c9885518SGarrett Wollmanis used to determine whether compatibility mode should be enabled.
165c9885518SGarrett WollmanThis feature is intended for use as a transition and debugging aid, when
16694a48596SGarrett Wollman.Nm
16794a48596SGarrett Wollmanis used in complex scripts which cannot easily be recast to avoid the
16894a48596SGarrett Wollmannon-portable usage.
169c9885518SGarrett WollmanEnabling compatibility mode
17094a48596SGarrett Wollmanalso implicitly enables the
17194a48596SGarrett Wollman.Fl e
17294a48596SGarrett Wollmanoption, since this matches the historic behavior of
17394a48596SGarrett Wollman.Nm
17494a48596SGarrett Wollmanin
17594a48596SGarrett Wollman.Fx .
176c9885518SGarrett WollmanFor historical reasons, defining the environment variable
177c9885518SGarrett Wollman.Ev EXPR_COMPAT
178c9885518SGarrett Wollmanalso enables compatibility mode.
17996ab7da3SGarrett Wollman.Sh ENVIRONMENT
1809dfcbc3bSRuslan Ermilov.Bl -tag -width ".Ev EXPR_COMPAT"
18196ab7da3SGarrett Wollman.It Ev EXPR_COMPAT
182c9885518SGarrett WollmanIf set, enables compatibility mode.
18396ab7da3SGarrett Wollman.El
184c9fe00dcSJ.T. Conklin.Sh EXAMPLES
185f477880aSGarrett Wollman.Bl -bullet
186c9fe00dcSJ.T. Conklin.It
187f07e4247SGarrett WollmanThe following example (in
188f07e4247SGarrett Wollman.Xr sh 1
189f07e4247SGarrett Wollmansyntax) adds one to the variable
190f477880aSGarrett Wollman.Va a :
1919dfcbc3bSRuslan Ermilov.Dl "a=$(expr $a + 1)"
192f477880aSGarrett Wollman.It
193dc2f94a3SGarrett WollmanThis will fail if the value of
194f07e4247SGarrett Wollman.Va a
195f07e4247SGarrett Wollmanis a negative number.
196f07e4247SGarrett WollmanTo protect negative values of
197f07e4247SGarrett Wollman.Va a
198f07e4247SGarrett Wollmanfrom being interpreted as options to the
199f07e4247SGarrett Wollman.Nm
200f07e4247SGarrett Wollmancommand, one might rearrange the expression:
2019dfcbc3bSRuslan Ermilov.Dl "a=$(expr 1 + $a)"
202f477880aSGarrett Wollman.It
203f07e4247SGarrett WollmanMore generally, parenthesize possibly-negative values:
2049dfcbc3bSRuslan Ermilov.Dl "a=$(expr \e( $a \e) + 1)"
205c9fe00dcSJ.T. Conklin.It
2061393277eSGarrett WollmanThis example prints the filename portion of a pathname stored
207f07e4247SGarrett Wollmanin variable
208f07e4247SGarrett Wollman.Va a .
209f07e4247SGarrett WollmanSince
210f07e4247SGarrett Wollman.Va a
211f07e4247SGarrett Wollmanmight represent the path
212f07e4247SGarrett Wollman.Pa / ,
213f07e4247SGarrett Wollmanit is necessary to prevent it from being interpreted as the division operator.
214f07e4247SGarrett WollmanThe
215f07e4247SGarrett Wollman.Li //
216f07e4247SGarrett Wollmancharacters resolve this ambiguity.
2179dfcbc3bSRuslan Ermilov.Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
218f477880aSGarrett Wollman.El
219f477880aSGarrett Wollman.Pp
220f07e4247SGarrett WollmanThe following examples output the number of characters in variable
221f07e4247SGarrett Wollman.Va a .
222f07e4247SGarrett WollmanAgain, if
223f07e4247SGarrett Wollman.Va a
224f07e4247SGarrett Wollmanmight begin with a hyphen, it is necessary to prevent it from being
225f07e4247SGarrett Wollmaninterpreted as an option to
226f07e4247SGarrett Wollman.Nm .
227f477880aSGarrett Wollman.Bl -bullet
228f477880aSGarrett Wollman.It
229f07e4247SGarrett WollmanIf the
230f07e4247SGarrett Wollman.Nm
231f07e4247SGarrett Wollmancommand conforms to
232f07e4247SGarrett Wollman.St -p1003.1-2001 ,
233f07e4247SGarrett Wollmanthis is simple:
2349dfcbc3bSRuslan Ermilov.Dl "expr -- \*q$a\*q \&: \*q.*\*q"
235f477880aSGarrett Wollman.It
236f07e4247SGarrett WollmanFor portability to older systems, however, a more complicated command
237f07e4247SGarrett Wollmanis required:
2389dfcbc3bSRuslan Ermilov.Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
239c9fe00dcSJ.T. Conklin.El
240c9fe00dcSJ.T. Conklin.Sh DIAGNOSTICS
241c9fe00dcSJ.T. ConklinThe
24215638d89SPhilippe Charnier.Nm
243c9fe00dcSJ.T. Conklinutility exits with one of the following values:
24415638d89SPhilippe Charnier.Bl -tag -width indent -compact
245c9fe00dcSJ.T. Conklin.It 0
246c9fe00dcSJ.T. Conklinthe expression is neither an empty string nor 0.
247c9fe00dcSJ.T. Conklin.It 1
248c9fe00dcSJ.T. Conklinthe expression is an empty string or 0.
249c9fe00dcSJ.T. Conklin.It 2
250c9fe00dcSJ.T. Conklinthe expression is invalid.
251c9fe00dcSJ.T. Conklin.El
252e5bf7bf4SJoseph Koshy.Sh SEE ALSO
253e5bf7bf4SJoseph Koshy.Xr sh 1 ,
254c9885518SGarrett Wollman.Xr test 1 ,
255c9885518SGarrett Wollman.Xr check_utility_compat 3
256c9fe00dcSJ.T. Conklin.Sh STANDARDS
257c9fe00dcSJ.T. ConklinThe
25815638d89SPhilippe Charnier.Nm
259d8c21a3eSJ.T. Conklinutility conforms to
26096ab7da3SGarrett Wollman.St -p1003.1-2001 ,
261c9885518SGarrett Wollmanprovided that compatibility mode is not enabled.
2621393277eSGarrett WollmanThe
2631393277eSGarrett Wollman.Fl e
2641393277eSGarrett Wollmanflag is an extension.
265