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