xref: /netbsd-src/bin/expr/expr.1 (revision 001c68bd94f75ce9270b69227c4199fbf34ee396)
1.\"	$NetBSD: expr.1,v 1.23 2003/03/29 18:25:22 perry Exp $
2.\"
3.\" Written by J.T. Conklin <jtc@NetBSD.org>.
4.\" Public domain.
5.\"
6.Dd September 18, 2000
7.Dt EXPR 1
8.Os
9.Sh NAME
10.Nm expr
11.Nd evaluate expression
12.Sh SYNOPSIS
13.Nm
14.Ar expression
15.Sh DESCRIPTION
16The
17.Nm
18utility evaluates
19.Ar expression
20and writes the result on standard output.
21.Pp
22All operators are separate arguments to the
23.Nm
24utility.
25Characters special to the command interpreter must be escaped.
26.Pp
27Operators are listed below in order of increasing precedence.
28Operators with equal precedence are grouped within { } symbols.
29.Bl -tag -width indent
30.It Ar expr1 Li | Ar expr2
31Returns the evaluation of
32.Ar expr1
33if it is neither an empty string nor zero;
34otherwise, returns the evaluation of
35.Ar expr2 .
36.It Ar expr1 Li \*[Am] Ar expr2
37Returns the evaluation of
38.Ar expr1
39if neither expression evaluates to an empty string or zero;
40otherwise, returns zero.
41.It Ar expr1 Li "{=, \*[Gt], \*[Ge], \*[Lt], \*[Le], !=}" Ar expr2
42Returns the results of integer comparison if both arguments are integers;
43otherwise, returns the results of string comparison using the locale-specific
44collation sequence.
45The result of each comparison is 1 if the specified relation is true,
46or 0 if the relation is false.
47.It Ar expr1 Li "{+, -}" Ar expr2
48Returns the results of addition or subtraction of integer-valued arguments.
49.It Ar expr1 Li "{*, /, %}" Ar expr2
50Returns the results of multiplication, integer division, or remainder of integer-valued arguments.
51.It Ar expr1 Li : Ar expr2
52The
53.Dq \&:
54operator matches
55.Ar expr1
56against
57.Ar expr2 ,
58which must be a regular expression.
59The regular expression is anchored
60to the beginning of  the string with an implicit
61.Dq ^ .
62.Pp
63If the match succeeds and the pattern contains at least one regular
64expression subexpression
65.Dq "\e(...\e)" ,
66the string corresponding to
67.Dq "\e1"
68is returned;
69otherwise the matching operator returns the number of characters matched.
70If the match fails and the pattern contains a regular expression subexpression
71the null string is returned;
72otherwise 0.
73.It Ar "( " expr Li " )"
74Parentheses are used for grouping in the usual manner.
75.El
76.Pp
77Operator precedence (from highest to lowest):
78.Bl -enum -compact -offset indent
79.It
80parentheses
81.It
82.Dq \&:
83.It
84.Dq "*" ,
85.Dq "/" ,
86and
87.Dq "%"
88.It
89.Dq "+"
90and
91.Dq "-"
92.It
93compare operators
94.It
95.Dq \*[Am]
96.It
97.Dq \Z'\*[tty-rn]'|
98.El
99.Sh EXIT STATUS
100The
101.Nm
102utility exits with one of the following values:
103.Bl -tag -width Ds -compact
104.It 0
105the expression is neither an empty string nor 0.
106.It 1
107the expression is an empty string or 0.
108.It 2
109the expression is invalid.
110.It \*[Gt]2
111an error occurred (such as memory allocation failure).
112.El
113.Sh EXAMPLES
114.Bl -enum
115.It
116The following example adds one to the variable a.
117.Dl a=`expr $a + 1`
118.It
119The following example returns zero, due to deduction having higher precedence
120than '\*[Am]' operator.
121.Dl expr 1 '\*[Am]' 1 - 1
122.It
123The following example returns the filename portion of a pathname stored
124in variable a.
125.Dl expr "/$a" Li : '.*/\e(.*\e)'
126.It
127The following example returns the number of characters in variable a.
128.Dl expr $a Li : '.*'
129.El
130.Sh STANDARDS
131The
132.Nm
133utility conforms to
134.St -p1003.2 .
135.Sh AUTHORS
136Original implementation was written by
137.An J.T. Conklin
138.Aq jtc@NetBSD.org .
139It was rewritten for
140.Nx 1.6
141by
142.An Jaromir Dolecek
143.Aq jdolecek@NetBSD.org .
144.Sh COMPATIBILITY
145This implementation of
146.Nm
147internally uses 64 bit representation of integers and checks for
148over- and underflows.
149It also treats / (division mark) and
150option '--' correctly depending upon context.
151.Pp
152.Nm
153on other systems (including
154.Nx
155up to and including
156.Nx 1.5 )
157might be not so graceful.
158Arithmetic results might be arbitrarily
159limited on such systems, most commonly to 32 bit quantities.
160This means such
161.Nm
162can only process values between -2147483648 and +2147483647.
163.Pp
164On other systems,
165.Nm
166might also not work correctly for regular expressions where
167either side contains single forward slash, like this:
168.Bd -literal -offset indent
169expr / : '.*/\e(.*\e)'
170.Ed
171.Pp
172If this is the case, you might use // (double forward slash)
173to avoid confusion with the division operator:
174.Bd -literal -offset indent
175expr "//$a" : '.*/\e(.*\e)'
176.Ed
177.Pp
178According to
179.St -p1003.2 ,
180.Nm
181has to recognize special option '--', treat it as an end of command
182line options and ignore it.
183Some
184.Nm
185implementations don't recognize it at all, others
186might ignore it even in cases where doing so results in syntax
187error.
188There should be same result for both following examples,
189but it might not always be:
190.Bl -enum -compact -offset indent
191.It
192expr -- : .
193.It
194expr -- -- : .
195.El
196Although
197.Nx
198.Nm
199handles both cases correctly, you should not depend on this behavior
200for portability reasons and avoid passing bare '--' as first
201argument.
202