]> git.saurik.com Git - apple/shell_cmds.git/blob - expr/expr.1
shell_cmds-74.1.tar.gz
[apple/shell_cmds.git] / expr / expr.1
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
16 The
17 .Nm
18 utility evaluates
19 .Ar expression
20 and writes the result on standard output.
21 .Pp
22 All operators are separate arguments to the
23 .Nm
24 utility.
25 Characters special to the command interpreter must be escaped.
26 .Pp
27 Operators are listed below in order of increasing precedence.
28 Operators with equal precedence are grouped within { } symbols.
29 .Bl -tag -width indent
30 .It Ar expr1 Li | Ar expr2
31 Returns the evaluation of
32 .Ar expr1
33 if it is neither an empty string nor zero;
34 otherwise, returns the evaluation of
35 .Ar expr2 .
36 .It Ar expr1 Li & Ar expr2
37 Returns the evaluation of
38 .Ar expr1
39 if neither expression evaluates to an empty string or zero;
40 otherwise, returns zero.
41 .It Ar expr1 Li "{=, >, >=, <, <=, !=}" Ar expr2
42 Returns the results of integer comparison if both arguments are integers;
43 otherwise, returns the results of string comparison using the locale-specific
44 collation sequence.
45 The result of each comparison is 1 if the specified relation is true,
46 or 0 if the relation is false.
47 .It Ar expr1 Li "{+, -}" Ar expr2
48 Returns the results of addition or subtraction of integer-valued arguments.
49 .It Ar expr1 Li "{*, /, %}" Ar expr2
50 Returns the results of multiplication, integer division, or remainder of integer-valued arguments.
51 .It Ar expr1 Li : Ar expr2
52 The
53 .Dq \&:
54 operator matches
55 .Ar expr1
56 against
57 .Ar expr2 ,
58 which must be a regular expression. The regular expression is anchored
59 to the beginning of the string with an implicit
60 .Dq ^ .
61 .Nm
62 expects "basic" regular expressions, see
63 .Xr re_format 7
64 for more information on regular expressions.
65 .Pp
66 If the match succeeds and the pattern contains at least one regular
67 expression subexpression
68 .Dq "\e(...\e)" ,
69 the string corresponding to
70 .Dq "\e1"
71 is returned;
72 otherwise the matching operator returns the number of characters matched.
73 If the match fails and the pattern contains a regular expression subexpression
74 the null string is returned;
75 otherwise 0.
76 .El
77 .Pp
78 Parentheses are used for grouping in the usual manner.
79 .Sh EXAMPLES
80 .Bl -enum
81 .It
82 The following example adds one to the variable a.
83 .Dl a=`expr $a + 1`
84 .It
85 The following example returns the filename portion of a pathname stored
86 in variable a. The // characters act to eliminate ambiguity with the
87 division operator.
88 .Dl expr "//$a" Li : '.*/\e(.*\e)'
89 .It
90 The following example returns the number of characters in variable a.
91 .Dl expr $a Li : '.*'
92 .El
93 .Sh DIAGNOSTICS
94 The
95 .Nm
96 utility exits with one of the following values:
97 .Bl -tag -width Ds -compact
98 .It 0
99 the expression is neither an empty string nor 0.
100 .It 1
101 the expression is an empty string or 0.
102 .It 2
103 the expression is invalid.
104 .El
105 .Sh STANDARDS
106 The
107 .Nm
108 utility conforms to
109 .St -p1003.2 .