# sbase

suckless unix tools
git clone git://git.2f30.org/sbase.git

expr.1 (1763B)

```      1 .Dd 2015-10-08
2 .Dt EXPR 1
3 .Os sbase
4 .Sh NAME
5 .Nm expr
6 .Nd evaluate expression
7 .Sh SYNOPSIS
8 .Nm
9 .Ar expression
10 .Sh DESCRIPTION
11 .Nm
12 evaluates
13 .Ar expression
14 and writes the result to stdout.
15 .Pp
16 There are two elemental expressions,
17 .Sy integer
18 and
19 .Sy string.
20 Let
21 .Sy expr
22 be a non-elemental expression and
23 .Sy expr1 ,
24 .Sy expr2
25 arbitrary expressions. Then
26 .Sy expr
27 has the recursive form
28 .Sy expr = [(] expr1 operand expr2 [)].
29 .Pp
30 With
31 .Sy operand
32 being in order of increasing precedence:
33 .Bl -tag -width Ds
34 .It |
35 Evaluate to
36 .Sy expr1
37 if it is neither an empty string nor 0; otherwise evaluate to
38 .Sy expr2 .
39 .It &
40 Evaluate to
41 .Sy expr1
42 if
43 .Sy expr1
44 and
45 .Sy expr2
46 are neither empty strings nor 0; otherwise evaluate to 0.
47 .It = > >= < <= !=
48 If
49 .Sy expr1
50 and
51 .Sy expr2
52 are integers, evaluate to 1 if the relation is true and 0 if it is false.
53 If
54 .Sy expr1
55 and
56 .Sy expr2
57 are strings, apply the relation to the return value of
58 .Xr strcmp 3 .
59 .It + -
60 If
61 .Sy expr1
62 and
63 .Sy expr2
64 are integers, evaluate to their sum or subtraction.
65 .It * / %
66 If
67 .Sy expr1
68 and
69 .Sy expr2
70 are integers, evaluate to their multiplication, division or remainder.
71 .It :
72 Evaluate to the number of characters matched in
73 .Sy expr1
74 against
75 .Sy expr2 . expr2
76 is anchored with an implicit '^'.
77 .Pp
78 You can't directly match the empty string, since zero matched characters
79 resolve equally to a failed match. To work around this limitation, use
80 "expr X'' : 'X\$' instead of "expr '' : '\$'"
81 .El
82 .Sh EXIT STATUS
83 .Bl -tag -width Ds
84 .It 0
85 .Ar expression
86 is neither an empty string nor 0.
87 .It 1
88 .Ar expression
89 is an empty string or 0.
90 .It 2
91 .Ar expression
92 is invalid.
93 .It > 2
94 An error occurred.
95 .El