xref: /openbsd-src/bin/expr/expr.1 (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1.\"	$OpenBSD: expr.1,v 1.18 2009/02/08 17:15:09 jmc 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: February 8 2009 $
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 Li | Ar 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 integers;
43otherwise, returns the results of string comparison using the locale-specific
44collation 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 integer-valued arguments.
49.It Ar expr1 Li "{*, /, %}" Ar expr2
50Returns the results of multiplication, integer division, or remainder of
51integer-valued arguments.
52.It Ar expr1 Li : Ar expr2
53The
54.Ql \&:
55operator matches
56.Ar expr1
57against
58.Ar expr2 ,
59which must be a basic regular expression.
60The regular expression is anchored
61to the beginning of the string with an implicit
62.Ql ^ .
63.Pp
64If the match succeeds and the pattern contains at least one regular
65expression subexpression
66.Dq "\e(...\e)" ,
67the string corresponding to
68.Dq "\e1"
69is returned;
70otherwise, the matching operator returns the number of characters matched.
71If the match fails and the pattern contains a regular expression subexpression
72the null string is returned;
73otherwise, returns 0.
74.Pp
75Note: the empty string cannot be matched using
76.Bd -literal -offset indent
77expr '' : '$'
78.Ed
79.Pp
80This is because the returned number of matched characters
81.Pq zero
82is indistinguishable from a failed match, so
83.Nm
84returns failure
85.Pq 0 .
86To match the empty string, use a structure such as:
87.Bd -literal -offset indent
88expr X'' : 'X$'
89.Ed
90.El
91.Pp
92Parentheses are used for grouping in the usual manner.
93.Sh EXAMPLES
94.Li $ a=`expr $a + 1`
95.Pp
96Add 1 to the variable
97.Va a .
98.Pp
99.Li $ expr "//$a" \&: '.*/\e(.*\e)'
100.Pp
101Return the filename portion of a pathname stored
102in variable
103.Va a .
104The
105.Ql //
106characters act to eliminate ambiguity with the division operator.
107.Pp
108.Li $ expr $a \&: '.*'
109.Pp
110Return the number of characters in variable
111.Va a .
112.Sh DIAGNOSTICS
113The
114.Nm
115utility exits with one of the following values:
116.Pp
117.Bl -tag -width Ds -compact
118.It 0
119The expression is neither an empty string nor 0.
120.It 1
121The expression is an empty string or 0.
122.It 2
123The expression is invalid.
124.It >2
125An error occurred (such as memory allocation failure).
126.El
127.Sh SEE ALSO
128.Xr test 1 ,
129.Xr re_format 7
130.Sh STANDARDS
131The
132.Nm
133utility is compliant with the
134.St -p1003.1-2008
135specification.
136