Merge from vendor branch GCC:
[dragonfly.git] / bin / expr / expr.1
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.9.2.4 2003/05/16 22:55:19 keramida Exp $
32 .\" $DragonFly: src/bin/expr/expr.1,v 1.2 2003/06/17 04:22:50 dillon Exp $
33 .\"
34 .Dd May 10, 2002
35 .Dt EXPR 1
36 .Os
37 .Sh NAME
38 .Nm expr
39 .Nd evaluate expression
40 .Sh SYNOPSIS
41 .Nm
42 .Ar expression
43 .Sh DESCRIPTION
44 The
45 .Nm
46 utility evaluates
47 .Ar expression
48 and writes the result on standard output.
49 .Pp
50 All operators and operands must be passed as separate arguments.
51 Several of the operators have special meaning to command interpreters
52 and must therefore be quoted appropriately.
53 All integer operands are interpreted in base 10.
54 .Pp
55 Arithmetic operations are performed using signed integer math.
56 .Pp
57 Operators are listed below in order of increasing precedence; all
58 are left-associative.
59 Operators with equal precedence are grouped within { } symbols.
60 .Bl -tag -width indent
61 .It Ar expr1 Li | Ar expr2
62 Return the evaluation of
63 .Ar expr1
64 if it is neither an empty string nor zero;
65 otherwise, returns the evaluation of
66 .Ar expr2 .
67 .It Ar expr1 Li & Ar expr2
68 Return the evaluation of
69 .Ar expr1
70 if neither expression evaluates to an empty string or zero;
71 otherwise, returns zero.
72 .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
73 Return the results of integer comparison if both arguments are integers;
74 otherwise, returns the results of string comparison using the locale-specific
75 collation sequence.
76 The result of each comparison is 1 if the specified relation is true,
77 or 0 if the relation is false.
78 .It Ar expr1 Li "{+, -}" Ar expr2
79 Return the results of addition or subtraction of integer-valued arguments.
80 .It Ar expr1 Li "{*, /, %}" Ar expr2
81 Return the results of multiplication, integer division, or remainder of integer-valued arguments.
82 .It Ar expr1 Li : Ar expr2
83 The
84 .Dq \&:
85 operator matches
86 .Ar expr1
87 against
88 .Ar expr2 ,
89 which must be a regular expression.  The regular expression is anchored
90 to the beginning of  the string with an implicit
91 .Dq ^ .
92 .Pp
93 If the match succeeds and the pattern contains at least one regular
94 expression subexpression
95 .Dq "\e(...\e)" ,
96 the string corresponding to
97 .Dq "\e1"
98 is returned;
99 otherwise the matching operator returns the number of characters matched.
100 If the match fails and the pattern contains a regular expression subexpression
101 the null string is returned;
102 otherwise 0.
103 .El
104 .Pp
105 Parentheses are used for grouping in the usual manner.
106 .Sh EXAMPLES
107 .Bl -enum
108 .It
109 The following example adds one to the variable a.
110 .Dl a=`expr $a + 1`
111 .It
112 The following example returns the filename portion of a pathname stored
113 in variable a.  The // characters act to eliminate ambiguity with the
114 division operator.
115 .Dl expr "//$a" Li : '.*/\e(.*\e)'
116 .It
117 The following example returns the number of characters in variable a.
118 .Dl expr $a Li : '.*'
119 .El
120 .Sh DIAGNOSTICS
121 The
122 .Nm
123 utility exits with one of the following values:
124 .Bl -tag -width indent -compact
125 .It 0
126 the expression is neither an empty string nor 0.
127 .It 1
128 the expression is an empty string or 0.
129 .It 2
130 the expression is invalid.
131 .El
132 .Sh SEE ALSO
133 .Xr sh 1 ,
134 .Xr test 1
135 .Sh STANDARDS
136 The
137 .Nm
138 utility conforms to
139 .St -p1003.2 .