1 .\" $NetBSD: expr.1,v 1.33 2012/08/12 17:27:04 wiz Exp $
3 .\" Copyright (c) 2000,2003 The NetBSD Foundation, Inc.
4 .\" All rights reserved.
6 .\" This code is derived from software contributed to The NetBSD Foundation
7 .\" by J.T. Conklin <jtc@NetBSD.org> and Jaromir Dolecek <jdolecek@NetBSD.org>.
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\" notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\" notice, this list of conditions and the following disclaimer in the
16 .\" documentation and/or other materials provided with the distribution.
18 .\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 .\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 .\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 .\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 .\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 .\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 .\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 .\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 .\" POSSIBILITY OF SUCH DAMAGE.
35 .Nd evaluate expression
44 and writes the result on standard output.
46 All operators are separate arguments to the
49 Characters special to the command interpreter must be escaped.
51 Operators are listed below in order of increasing precedence.
52 Operators with equal precedence are grouped within { } symbols.
53 .Bl -tag -width indent
54 .It Ar expr1 Li \&| Ar expr2
55 Returns the evaluation of
57 if it is neither an empty string nor zero;
58 otherwise, returns the evaluation of
60 .It Ar expr1 Li \*[Am] Ar expr2
61 Returns the evaluation of
63 if neither expression evaluates to an empty string or zero;
64 otherwise, returns zero.
65 .It Ar expr1 Li "{=, \*[Gt], \*[Ge], \*[Lt], \*[Le], !=}" Ar expr2
66 Returns the results of integer comparison if both arguments are integers;
67 otherwise, returns the results of string comparison using the locale-specific
69 The result of each comparison is 1 if the specified relation is true,
70 or 0 if the relation is false.
71 .It Ar expr1 Li "{+, -}" Ar expr2
72 Returns the results of addition or subtraction of integer-valued arguments.
73 .It Ar expr1 Li "{*, /, %}" Ar expr2
74 Returns the results of multiplication, integer division, or remainder of integer-valued arguments.
75 .It Ar expr1 Li \&: Ar expr2
82 which must be a regular expression.
83 The regular expression is anchored
84 to the beginning of the string with an implicit
87 If the match succeeds and the pattern contains at least one regular
88 expression subexpression
90 the string corresponding to
93 otherwise the matching operator returns the number of characters matched.
94 If the match fails and the pattern contains a regular expression subexpression
95 the null string is returned;
97 .It "( " Ar expr No " )"
98 Parentheses are used for grouping in the usual manner.
101 Additionally, the following keywords are recognized:
102 .Bl -tag -width indent
104 Returns the length of the specified string in bytes.
107 Operator precedence (from highest to lowest):
108 .Bl -enum -compact -offset indent
134 utility exits with one of the following values:
135 .Bl -tag -width Ds -compact
137 the expression is neither an empty string nor 0.
139 the expression is an empty string or 0.
141 the expression is invalid.
143 an error occurred (such as memory allocation failure).
148 The following example adds one to variable
152 The following example returns zero, due to subtraction having higher precedence
156 .Dl expr 1 '\*[Am]' 1 - 1
158 The following example returns the filename portion of a pathname stored
161 .Dl expr "/$a" Li : '.*/\e(.*\e)'
163 The following example returns the number of characters in variable
165 .Dl expr $a Li : '.*'
168 This implementation of
170 internally uses 64 bit representation of integers and checks for
171 over- and underflows.
174 (the division mark) and option
176 correctly depending upon context.
179 on other systems (including
183 might not be so graceful.
184 Arithmetic results might be arbitrarily
185 limited on such systems, most commonly to 32 bit quantities.
188 can only process values between -2147483648 and +2147483647.
192 might also not work correctly for regular expressions where
195 (a single forward slash), like this:
196 .Bd -literal -offset indent
197 expr / : '.*/\e(.*\e)'
200 If this is the case, you might use
202 (a double forward slash)
203 to avoid confusion with the division operator:
204 .Bd -literal -offset indent
205 expr "//$a" : '.*/\e(.*\e)'
211 has to recognize special option
213 treat it as a delimiter to mark the end of command
214 line options, and ignore it.
217 implementations don't recognize it at all; others
218 might ignore it even in cases where doing so results in syntax
220 There should be same result for both following examples,
221 but it might not always be:
222 .Bl -enum -compact -offset indent
231 handles both cases correctly, you should not depend on this behavior
232 for portability reasons and avoid passing a bare
243 keyword is an extension for compatibility with GNU
246 Original implementation was written by
253 .Aq jdolecek@NetBSD.org .
257 cannot be matched with the intuitive:
258 .Bd -literal -offset indent
262 The reason is that the returned number of matched characters (zero)
263 is indistinguishable from a failed match, so this returns failure.
264 To match the empty string, use something like:
265 .Bd -literal -offset indent