]> git.saurik.com Git - apple/shell_cmds.git/blame_incremental - expr/expr.1
shell_cmds-207.11.1.tar.gz
[apple/shell_cmds.git] / expr / expr.1
... / ...
CommitLineData
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: src/bin/expr/expr.1,v 1.31 2011/07/09 12:05:53 se Exp $
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.Ar expression
42.Sh DESCRIPTION
43The
44.Nm
45utility evaluates
46.Ar expression
47and writes the result on standard output.
48.Pp
49All operators and operands must be passed as separate arguments.
50Several of the operators have special meaning to command interpreters
51and must therefore be quoted appropriately.
52All integer operands are interpreted in base 10 and must consist of only
53an optional leading minus sign followed by one or more digits.
54.Pp
55Arithmetic operations are performed using signed integer math with a
56range according to the C
57.Vt intmax_t
58data type (the largest signed integral type available).
59All conversions and operations are checked for overflow.
60Overflow results in program termination with an error message on stdout
61and with an error status.
62.Pp
63Operators are listed below in order of increasing precedence; all
64are left-associative.
65Operators with equal precedence are grouped within symbols
66.Ql {
67and
68.Ql } .
69.Bl -tag -width indent
70.It Ar expr1 Li | Ar expr2
71Return the evaluation of
72.Ar expr1
73if it is neither an empty string nor zero;
74otherwise, returns the evaluation of
75.Ar expr2
76if it is not an empty string;
77otherwise, returns zero.
78.It Ar expr1 Li & Ar expr2
79Return the evaluation of
80.Ar expr1
81if neither expression evaluates to an empty string or zero;
82otherwise, returns zero.
83.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
84Return the results of integer comparison if both arguments are integers;
85otherwise, returns the results of string comparison using the locale-specific
86collation sequence.
87The result of each comparison is 1 if the specified relation is true,
88or 0 if the relation is false.
89.It Ar expr1 Li "{+, -}" Ar expr2
90Return the results of addition or subtraction of integer-valued arguments.
91.It Ar expr1 Li "{*, /, %}" Ar expr2
92Return the results of multiplication, integer division, or remainder of integer-valued arguments.
93.It Ar expr1 Li : Ar expr2
94The
95.Dq Li \&:
96operator matches
97.Ar expr1
98against
99.Ar expr2 ,
100which must be a basic regular expression.
101The regular expression is anchored
102to the beginning of the string with an implicit
103.Dq Li ^ .
104.Pp
105If the match succeeds and the pattern contains at least one regular
106expression subexpression
107.Dq Li "\e(...\e)" ,
108the string corresponding to
109.Dq Li \e1
110is returned;
111otherwise the matching operator returns the number of characters matched.
112If the match fails and the pattern contains a regular expression subexpression
113the null string is returned;
114otherwise 0.
115.El
116.Pp
117Parentheses are used for grouping in the usual manner.
118.Pp
119The
120.Nm
121utility makes no lexical distinction between arguments which may be
122operators and arguments which may be operands.
123An operand which is lexically identical to an operator will be considered a
124syntax error.
125See the examples below for a work-around.
126.Pp
127The syntax of the
128.Nm
129command in general is historic and inconvenient.
130New applications are advised to use shell arithmetic rather than
131.Nm .
132.Sh EXIT STATUS
133The
134.Nm
135utility exits with one of the following values:
136.Bl -tag -width indent -compact
137.It 0
138the expression is neither an empty string nor 0.
139.It 1
140the expression is an empty string or 0.
141.It 2
142the expression is invalid.
143.El
144.Sh EXAMPLES
145.Bl -bullet
146.It
147The following example (in
148.Xr sh 1
149syntax) adds one to the variable
150.Va a :
151.Dl "a=$(expr $a + 1)"
152.It
153This will fail if the value of
154.Va a
155is a negative number.
156To protect negative values of
157.Va a
158from being interpreted as options to the
159.Nm
160command, one might rearrange the expression:
161.Dl "a=$(expr 1 + $a)"
162.It
163More generally, parenthesize possibly-negative values:
164.Dl "a=$(expr \e( $a \e) + 1)"
165.It
166With shell arithmetic, no escaping is required:
167.Dl "a=$((a + 1))"
168.It
169This example prints the filename portion of a pathname stored
170in variable
171.Va a .
172Since
173.Va a
174might represent the path
175.Pa / ,
176it is necessary to prevent it from being interpreted as the division operator.
177The
178.Li //
179characters resolve this ambiguity.
180.Dl "expr \*q//$a\*q \&: '.*/\e(.*\e)'"
181.It
182With modern
183.Xr sh 1
184syntax,
185.Dl "\*q${a##*/}\*q"
186expands to the same value.
187.El
188.Pp
189The following examples output the number of characters in variable
190.Va a .
191Again, if
192.Va a
193might begin with a hyphen, it is necessary to prevent it from being
194interpreted as an option to
195.Nm ,
196and
197.Va a
198might be interpreted as an operator.
199.Bl -bullet
200.It
201To deal with all of this, a complicated command
202is required:
203.Dl "expr \e( \*qX$a\*q \&: \*q.*\*q \e) - 1"
204.It
205With modern
206.Xr sh 1
207syntax, this can be done much more easily:
208.Dl "${#a}"
209expands to the required number.
210.El
211.Sh SEE ALSO
212.Xr sh 1 ,
213.Xr test 1
214.Sh STANDARDS
215The
216.Nm
217utility conforms to
218.St -p1003.1-2008 .
219.Pp
220The extended arithmetic range and overflow checks do not conflict with
221POSIX's requirement that arithmetic be done using signed longs, since
222they only make a difference to the result in cases where using signed
223longs would give undefined behavior.
224.Pp
225According to the
226.Tn POSIX
227standard, the use of string arguments
228.Va length ,
229.Va substr ,
230.Va index ,
231or
232.Va match
233produces undefined results. In this version of
234.Nm ,
235these arguments are treated just as their respective string values.