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