xref: /netbsd-src/bin/expr/expr.1 (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
1.\"	$NetBSD: expr.1,v 1.8 1995/03/21 09:05:37 cgd Exp $
2.\"
3.\" -*- nroff -*-
4.\"
5.\" Copyright (c) 1993 Winning Strategies, Inc.
6.\" All rights reserved.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. All advertising materials mentioning features or use of this software
17.\"    must display the following acknowledgement:
18.\"      This product includes software developed by Winning Strategies, Inc.
19.\" 4. The name of the author may not be used to endorse or promote products
20.\"    derived from this software without specific prior written permission
21.\"
22.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32.\"
33.Dd July 3, 1993
34.Dt EXPR 1
35.Os
36.Sh NAME
37.Nm expr
38.Nd evaluate expression
39.Sh SYNOPSIS
40.Nm expr
41.Ar expression
42.Sh DESCRIPTION
43The
44.Nm expr
45utility evaluates
46.Ar expression
47and writes the result on standard output.
48.Pp
49All operators are separate arguments to the
50.Nm expr
51utility.
52Characters special to the command interpreter must be escaped.
53.Pp
54Operators are listed below in order of increasing precedence.
55Operators with equal precedence are grouped within { } symbols.
56.Bl -tag -width indent
57.It Ar expr1 Li | Ar expr2
58Returns the evaluation of
59.Ar expr1
60if it is neither an empty string nor zero;
61otherwise, returns the evaluation of
62.Ar expr2 .
63.It Ar expr1 Li & Ar expr2
64Returns the evaluation of
65.Ar expr1
66if neither expression evaluates to an empty string or zero;
67otherwise, returns zero.
68.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
69Returns the results of integer comparison if both arguments are integers;
70otherwise, returns the results of string comparison using the locale-specific
71collation sequence.
72The result of each comparison is 1 if the specified relation is true,
73or 0 if the relation is false.
74.It Ar expr1 Li "{+, -}" Ar expr2
75Returns the results of addition or subtraction of integer-valued arguments.
76.It Ar expr1 Li "{*, /, %}" Ar expr2
77Returns the results of multiplication, integer division, or remainder of integer-valued arguments.
78.It Ar expr1 Li : Ar expr2
79The
80.Dq \:
81operator matches
82.Ar expr1
83against
84.Ar expr2 ,
85which must be a regular expression.  The regular expression is anchored
86to the beginning of  the string with an implicit
87.Dq ^ .
88.Pp
89If the match succeeds and the pattern contains at least one regular
90expression subexpression
91.Dq "\e(...\e)" ,
92the string corresponding to
93.Dq "\e1"
94is returned;
95otherwise the matching operator returns the number of characters matched.
96If the match fails and the pattern contains a regular expression subexpression
97the null string is returned;
98otherwise 0.
99.El
100.Pp
101Parentheses are used for grouping in the usual manner.
102.Sh EXAMPLES
103.Bl -enum
104.It
105The following example adds one to the variable a.
106.Dl a=`expr $a + 1`
107.It
108The following example returns the filename portion of a pathname stored
109in variable a.  The // characters act to eliminate ambiguity with the
110division operator.
111.Dl expr "//$a" Li : '.*/\e(.*\e)'
112.It
113The following example returns the number of characters in variable a.
114.Dl expr $a Li : '.*'
115.El
116.Sh DIAGNOSTICS
117The
118.Nm expr
119utility exits with one of the following values:
120.Bl -tag -width Ds -compact
121.It 0
122the expression is neither an empty string nor 0.
123.It 1
124the expression is an empty string or 0.
125.It 2
126the expression is invalid.
127.El
128.Sh STANDARDS
129The
130.Nm expr
131utility conforms to
132.St -p1003.2 .
133