xref: /openbsd/bin/expr/expr.1 (revision 76d0caae)
1.\"	$OpenBSD: expr.1,v 1.24 2017/08/16 20:10:58 schwarze Exp $
2.\"	$NetBSD: expr.1,v 1.9 1995/04/28 23:27:13 jtc Exp $
3.\"
4.\" Written by J.T. Conklin <jtc@netbsd.org>.
5.\" Public domain.
6.\"
7.Dd $Mdocdate: August 16 2017 $
8.Dt EXPR 1
9.Os
10.Sh NAME
11.Nm expr
12.Nd evaluate expression
13.Sh SYNOPSIS
14.Nm expr
15.Ar expression
16.Sh DESCRIPTION
17The
18.Nm
19utility evaluates
20.Ar expression
21and writes the result on standard output.
22All operators are separate arguments to the
23.Nm
24utility.
25Characters special to the command interpreter must be escaped.
26.Pp
27Operators are listed below in order of increasing precedence.
28Operators with equal precedence are grouped within { } symbols.
29.Bl -tag -width indent
30.It Ar expr1 | expr2
31Returns the evaluation of
32.Ar expr1
33if it is neither an empty string nor zero;
34otherwise, returns the evaluation of
35.Ar expr2 .
36.It Ar expr1 Li & Ar expr2
37Returns the evaluation of
38.Ar expr1
39if neither expression evaluates to an empty string or zero;
40otherwise, returns zero.
41.It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
42Returns the results of integer comparison if both arguments are
43decimal integers; otherwise, returns the results of string comparison
44using the locale-specific collation sequence.
45The result of each comparison is 1 if the specified relation is true,
46or 0 if the relation is false.
47.It Ar expr1 Li "{+, -}" Ar expr2
48Returns the results of addition or subtraction of decimal integer-valued
49arguments.
50.It Ar expr1 Li "{*, /, %}" Ar expr2
51Returns the results of multiplication, integer division, or remainder of
52decimal integer-valued arguments.
53.It Ar expr1 Li \&: Ar expr2
54The
55.Ql \&:
56operator matches
57.Ar expr1
58against
59.Ar expr2 ,
60which must be a basic regular expression.
61The regular expression is anchored
62to the beginning of the string with an implicit
63.Ql ^ .
64.Pp
65If the match succeeds and the pattern contains at least one regular
66expression subexpression
67.Dq "\e(...\e)" ,
68the string corresponding to
69.Dq "\e1"
70is returned;
71otherwise, the matching operator returns the number of characters matched.
72If the match fails and the pattern contains a regular expression subexpression
73the null string is returned;
74otherwise, returns 0.
75.Pp
76Note: the empty string cannot be matched using
77.Bd -literal -offset indent
78expr '' : '$'
79.Ed
80.Pp
81This is because the returned number of matched characters
82.Pq zero
83is indistinguishable from a failed match, so
84.Nm
85returns failure
86.Pq 0 .
87To match the empty string, use a structure such as:
88.Bd -literal -offset indent
89expr X'' : 'X$'
90.Ed
91.El
92.Pp
93Parentheses are used for grouping in the usual manner.
94.Sh EXIT STATUS
95The
96.Nm
97utility exits with one of the following values:
98.Pp
99.Bl -tag -width Ds -offset indent -compact
100.It 0
101The expression is neither an empty string nor 0.
102.It 1
103The expression is an empty string or 0.
104.It 2
105The expression is invalid.
106.It \*(Gt2
107An error occurred (such as memory allocation failure).
108.El
109.Sh EXAMPLES
110Add 1 to the variable
111.Va a :
112.Bd -literal -offset indent
113$ a=`expr $a + 1`
114.Ed
115.Pp
116Return the filename portion of a pathname stored
117in variable
118.Va a .
119The
120.Ql //
121characters act to eliminate ambiguity with the division operator:
122.Bd -literal -offset indent
123$ expr "//$a" \&: '.*/\e(.*\e)'
124.Ed
125.Pp
126Return the number of characters in variable
127.Va a :
128.Bd -literal -offset indent
129$ expr $a \&: '.*'
130.Ed
131.Sh SEE ALSO
132.Xr test 1 ,
133.Xr re_format 7
134.Sh STANDARDS
135The
136.Nm
137utility is compliant with the
138.St -p1003.1-2008
139specification.
140.Sh HISTORY
141The
142.Nm
143utility first appeared in the Programmer's Workbench (PWB/UNIX)
144and has supported regular expressions since
145.At v7 .
146It was rewritten from scratch for
147.Bx 386 0.1
148and again for
149.Nx 1.1 .
150.Sh AUTHORS
151.An -nosplit
152The first free version was written by
153.An Pace Willisson
154in 1992.
155This version was written by
156.An John T. Conklin
157in 1994.
158