]> git.saurik.com Git - apple/shell_cmds.git/blame - expr/expr.1
shell_cmds-118.tar.gz
[apple/shell_cmds.git] / expr / expr.1
CommitLineData
44bd5ea7
A
1.\" $NetBSD: expr.1,v 1.10 1997/10/20 08:51:56 enami Exp $
2.\"
3.\" Written by J.T. Conklin <jtc@netbsd.org>.
4.\" Public domain.
5.\"
6.Dd July 3, 1993
7.Dt EXPR 1
8.Os
9.Sh NAME
10.Nm expr
11.Nd evaluate expression
12.Sh SYNOPSIS
13.Nm
14.Ar expression
15.Sh DESCRIPTION
16The
17.Nm
18utility evaluates
19.Ar expression
20and writes the result on standard output.
21.Pp
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 integer-valued arguments.
51.It Ar expr1 Li : Ar expr2
52The
9bafe280 53.Dq \&:
44bd5ea7
A
54operator matches
55.Ar expr1
56against
57.Ar expr2 ,
58which must be a regular expression. The regular expression is anchored
59to the beginning of the string with an implicit
60.Dq ^ .
9bafe280
A
61.Nm
62expects "basic" regular expressions, see
63.Xr re_format 7
64for more information on regular expressions.
44bd5ea7
A
65.Pp
66If the match succeeds and the pattern contains at least one regular
67expression subexpression
68.Dq "\e(...\e)" ,
69the string corresponding to
70.Dq "\e1"
71is returned;
72otherwise the matching operator returns the number of characters matched.
73If the match fails and the pattern contains a regular expression subexpression
74the null string is returned;
75otherwise 0.
76.El
77.Pp
78Parentheses are used for grouping in the usual manner.
79.Sh EXAMPLES
80.Bl -enum
81.It
82The following example adds one to the variable a.
83.Dl a=`expr $a + 1`
84.It
85The following example returns the filename portion of a pathname stored
86in variable a. The // characters act to eliminate ambiguity with the
87division operator.
88.Dl expr "//$a" Li : '.*/\e(.*\e)'
89.It
90The following example returns the number of characters in variable a.
91.Dl expr $a Li : '.*'
92.El
93.Sh DIAGNOSTICS
94The
95.Nm
96utility exits with one of the following values:
97.Bl -tag -width Ds -compact
98.It 0
99the expression is neither an empty string nor 0.
100.It 1
101the expression is an empty string or 0.
102.It 2
103the expression is invalid.
104.El
105.Sh STANDARDS
106The
107.Nm
108utility conforms to
109.St -p1003.2 .