xref: /dragonfly/bin/expr/expr.1 (revision 017817f0)
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.\"
33.Dd August 23, 2016
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.
53.Pp
54Arithmetic operations are performed using signed integer math.
55.Pp
56Operators are listed below in order of increasing precedence; all
57are left-associative.
58Operators with equal precedence are grouped within { } symbols.
59.Bl -tag -width indent
60.It Ar expr1 | expr2
61Return the evaluation of
62.Ar expr1
63if it is neither an empty string nor zero;
64otherwise, returns the evaluation of
65.Ar expr2 .
66.It Ar expr1 Li & Ar expr2
67Return the evaluation of
68.Ar expr1
69if neither expression evaluates to an empty string or zero;
70otherwise, returns zero.
71.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
72Return the results of integer comparison if both arguments are integers;
73otherwise, returns the results of string comparison using the locale-specific
74collation sequence.
75The result of each comparison is 1 if the specified relation is true,
76or 0 if the relation is false.
77.It Ar expr1 Li "{+, -}" Ar expr2
78Return the results of addition or subtraction of integer-valued arguments.
79.It Ar expr1 Li "{*, /, %}" Ar expr2
80Return the results of multiplication, integer division, or remainder of integer-valued arguments.
81.It Ar expr1 Li : Ar expr2
82The
83.Dq \&:
84operator matches
85.Ar expr1
86against
87.Ar expr2 ,
88which must be a regular expression.  The regular expression is anchored
89to the beginning of  the string with an implicit
90.Dq ^ .
91.Pp
92If the match succeeds and the pattern contains at least one regular
93expression subexpression
94.Dq "\e(...\e)" ,
95the string corresponding to
96.Dq "\e1"
97is returned;
98otherwise the matching operator returns the number of characters matched.
99If the match fails and the pattern contains a regular expression subexpression
100the null string is returned;
101otherwise 0.
102.El
103.Pp
104Parentheses are used for grouping in the usual manner.
105.Sh EXIT STATUS
106The
107.Nm
108utility exits with one of the following values:
109.Bl -tag -width indent -compact
110.It 0
111the expression is neither an empty string nor 0.
112.It 1
113the expression is an empty string or 0.
114.It 2
115the expression is invalid.
116.El
117.Sh EXAMPLES
118.Bl -enum
119.It
120The following example adds one to the variable a.
121.Dl a=`expr $a + 1`
122.It
123The following example returns the filename portion of a pathname stored
124in variable a.  The // characters act to eliminate ambiguity with the
125division operator.
126.Dl expr "//$a" Li : '.*/\e(.*\e)'
127.It
128The following example returns the number of characters in variable a.
129.Dl expr $a Li : '.*'
130.El
131.Sh SEE ALSO
132.Xr sh 1 ,
133.Xr test 1
134.Sh STANDARDS
135The
136.Nm
137utility conforms to
138.St -p1003.2 .
139.Sh HISTORY
140An
141.Nm
142utility first appeared in the Programmer's Workbench (PWB/UNIX).
143A public domain version of
144.Nm
145written by
146.An Pace Willisson
147.Aq Mt pace@blitz.com
148appeared in
149.Bx 386 0.1 .
150.Sh AUTHORS
151Initial implementation by
152.An Pace Willisson
153.Aq Mt pace@blitz.com
154was largely rewritten by
155.An -nosplit
156.An J.T. Conklin
157.Aq Mt jtc@FreeBSD.org .
158