expr.1 - sbase - suckless unix tools
 (HTM) git clone git://git.suckless.org/sbase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       expr.1 (1713B)
       ---
            1 .Dd October 8, 2015
            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.
           26 Then
           27 .Sy expr
           28 has the recursive form
           29 .Sy expr = [(] expr1 operand expr2 [)] .
           30 .Pp
           31 With
           32 .Sy operand
           33 being in order of increasing precedence:
           34 .Bl -tag -width Ds
           35 .It |
           36 Evaluate to
           37 .Sy expr1
           38 if it is neither an empty string nor 0; otherwise evaluate to
           39 .Sy expr2 .
           40 .It &
           41 Evaluate to
           42 .Sy expr1
           43 if
           44 .Sy expr1
           45 and
           46 .Sy expr2
           47 are neither empty strings nor 0; otherwise evaluate to 0.
           48 .It = > >= < <= !=
           49 If
           50 .Sy expr1
           51 and
           52 .Sy expr2
           53 are integers, evaluate to 1 if the relation is true and 0 if it is false.
           54 If
           55 .Sy expr1
           56 and
           57 .Sy expr2
           58 are strings, apply the relation to the return value of
           59 .Xr strcmp 3 .
           60 .It + -
           61 If
           62 .Sy expr1
           63 and
           64 .Sy expr2
           65 are integers, evaluate to their sum or subtraction.
           66 .It * / %
           67 If
           68 .Sy expr1
           69 and
           70 .Sy expr2
           71 are integers, evaluate to their multiplication, division or remainder.
           72 .It :
           73 Evaluate to the number of characters matched in
           74 .Sy expr1
           75 against
           76 .Sy expr2 . expr2
           77 is anchored with an implicit '^'.
           78 .Pp
           79 You can't directly match the empty string, since zero matched characters
           80 resolve equally to a failed match.
           81 To work around this limitation, use "expr X'' : 'X$' instead of "expr ''
           82 : '$'"
           83 .El
           84 .Sh EXIT STATUS
           85 .Bl -tag -width Ds
           86 .It 0
           87 .Ar expression
           88 is neither an empty string nor 0.
           89 .It 1
           90 .Ar expression
           91 is an empty string or 0.
           92 .It 2
           93 .Ar expression
           94 is invalid.
           95 .It > 2
           96 An error occurred.
           97 .El
           98 .Sh SEE ALSO
           99 .Xr test 1
          100 .Sh STANDARDS
          101 POSIX.1-2013.