EXPR(1)

NAME
expr - evaluate integer, string, and regular-expression expressions

SYNOPSIS
expr [-e] [--] expression ...

DESCRIPTION
expr evaluates the operand vector given on the command line and writes the result to standard output. Each operator and operand must be passed as a separate shell argument.

This Linux port is a standalone implementation derived from FreeBSD semantics, but it does not depend on the FreeBSD build system or compatibility probes.

OPTIONS

-e  Enable compatibility parsing for numeric operands. In this mode, expr accepts leading white space, an optional leading plus sign, and an empty string in numeric context.

--  End option processing. Use -- when the first operand may begin with ‘-’.

OPERANDS
Operators are evaluated from low precedence to high precedence in the following order. Operators with the same precedence associate left to right.

expr1 | expr2
    Return expr1 if it is neither the empty string nor numeric zero. Otherwise return expr2 if it is non-empty and non-zero. If both sides are null or zero, return 0.

expr1 & expr2
    Return expr1 if both operands are non-empty and non-zero. Otherwise return 0.

expr1 = expr2

expr1 != expr2

expr1 < expr2

expr1 <= expr2

expr1 > expr2

expr1 >= expr2
    If both operands are valid integers, compare them numerically. Otherwise compare them as strings using the current locale collation rules. The result is 1 for true and 0 for false.

expr1 + expr2

expr1 - expr2

expr1 * expr2

expr1 / expr2

expr1 % expr2
    Perform checked arithmetic on intmax_t. Division and remainder by zero are rejected. Overflow terminates the utility with an error.

expr1: expr2
    Match expr1 against the basic regular expression expr2. The match is anchored at the start of the string. If the pattern contains a capturing subexpression and the match succeeds, the first captured substring is returned. If the pattern has no capturing subexpression and the match succeeds, the length of the match is returned. On a failed match, the result is the empty string when the pattern has a capturing subexpression, otherwise 0.

Parentheses may be used for grouping. Because expr does not lexically separate operators from operands, a token that is identical to an operator must be disambiguated by shell quoting or by rewriting the expression.

SEMANTICS
By default, integer operands must be base-10 strings consisting of an optional leading minus sign followed by digits. In compatibility mode, -e, leading white space and a leading plus sign are accepted, and the empty string is treated as zero in numeric context.

This Linux port supports -- for POSIX-style end-of-options handling. It does not implement FreeBSD check_utility_compat(3) probing or the historical EXPR_COMPAT environment switch.

EXIT STATUS
0   The final value is neither empty nor zero.
1   The final value is the empty string or numeric zero.
2   The expression is invalid or evaluation failed.

DIAGNOSTICS
Errors are reported on standard error. The implementation emits explicit diagnostics for syntax errors, invalid decimal operands, arithmetic overflow, division by zero, regular-expression compilation failures, and write failures.

EXAMPLES

•   Increment a shell variable:
        n=$(expr $n + 1)

•   Safely handle a leading negative operand:
        n=$(expr -- $n + 1)

•   Return the basename component of a path:
        expr //$path: '.*/\(.*\)'

•   Return the length of a shell variable:
        expr (X$text: '.*') - 1

SEE ALSO
sh(1), test(1), regex(3)

STANDARDS
The strict parsing mode follows the POSIX utility syntax model for option handling and preserves the historical expr operator set. Compatibility mode (-e) is a local extension for FreeBSD-script migration.