LinuxCommandLibrary

expr

Evaluate arithmetic or string expressions

TLDR

Get the length of a specific string

$ expr length "[string]"
copy

Get the substring of a string with a specific length
$ expr substr "[string]" [from] [length]
copy

Match a specific substring against an anchored pattern
$ expr match "[string]" '[pattern]'
copy

Get the first char position from a specific set in a string
$ expr index "[string]" "[chars]"
copy

Calculate a specific mathematic expression
$ expr [expression1] [+|-|*|/|%] [expression2]
copy

Get the first expression if its value is non-zero and not null otherwise get the second one
$ expr [expression1] \| [expression2]
copy

Get the first expression if both expressions are non-zero and not null otherwise get zero
$ expr [expression1] \& [expression2]
copy

SYNOPSIS

expr EXPRESSION
expr OPTION

Where EXPRESSION is a sequence of arguments representing operands and operators. Operators and operands must be separated by whitespace. Shell metacharacters must be escaped or quoted.

PARAMETERS

--help
    Display a help message and exit.

--version
    Output version information and exit.

DESCRIPTION

expr is a command-line utility in Unix-like operating systems used to evaluate expressions and print the result to standard output. It can perform arithmetic operations (addition, subtraction, multiplication, division, modulo), relational comparisons, logical operations, and various string manipulations (like pattern matching, extracting substrings, and finding string length).

Unlike shell's built-in arithmetic expansions (e.g., `((...))` in Bash), expr is a standalone utility, making it useful in older shells or for more complex string operations that might be cumbersome with shell globbing. Each operator and operand must be a separate argument to the command. Special shell characters like `*`, `(`, `)` must be escaped or quoted to prevent the shell from interpreting them.

The command's exit status provides information about the result: 0 if the expression evaluates to a non-zero or non-null value, 1 if it evaluates to zero or null, and 2 if there's an error in the expression.

CAVEATS


Shell Metacharacters: Many operators (e.g., `*`, `(`, `)`, `>`, `<`, `|`, `&`) are shell metacharacters and must be escaped (`\*`) or quoted (`'*'`) to prevent the shell from interpreting them before expr sees them.

Argument Separation: Every operand and operator must be a separate argument. For example, `expr 1+2` is incorrect; it should be `expr 1 + 2`.

Integer Arithmetic: By default, expr primarily performs integer arithmetic. Division (`/`) truncates to an integer.

Performance: For simple integer arithmetic, shell built-in expansions (like `((...))` or `$((...))` in Bash) are generally more efficient and convenient than invoking an external command like expr.

OPERATORS

expr supports various types of operators, evaluated from left to right, with specific precedence rules. Parentheses `(` `)` can be used to override precedence, but they must be escaped or quoted.

Arithmetic Operators:
`+` (addition), `-` (subtraction), `*` (multiplication), `/` (division), `%` (remainder/modulo).

Relational Operators:
`=` (equal), `!=` (not equal), `<` (less than), `<=` (less than or equal), `>` (greater than), `>=` (greater than or equal). These return 1 if true, 0 if false.

Logical Operators:
`|` (OR): Returns the first non-null/non-zero argument, else the second. `&` (AND): Returns the first argument if neither is null/zero, else 0. (These often need escaping or quoting)

String Operators:
`EXPRESSION : PATTERN` (match): Matches EXPRESSION against PATTERN (a regular expression). Returns the length of the matched string, or 0 if no match. If part of the pattern is enclosed in `\( ... \)`, it returns the substring matched by that part.
`length STRING`: Returns the length of STRING.
`substr STRING POSITION LENGTH`: Returns a substring of STRING starting at POSITION for LENGTH characters.
`index STRING CHARSET`: Returns the first position in STRING where any character from CHARSET is found, or 0 if not found.
`match STRING PATTERN`: Equivalent to the `STRING : PATTERN` operator.

EXIT STATUS

expr uses its exit status to indicate the outcome of the evaluation:
0: The expression evaluated to a non-zero, non-null string.
1: The expression evaluated to zero or a null string.
2: An invalid expression was given, or an error occurred.

HISTORY

expr has been a fundamental utility in Unix-like operating systems since its early days, predating many of the advanced arithmetic and string manipulation capabilities now built into modern shells like Bash or Zsh. Its existence reflects a time when shells were simpler and external commands were necessary for even basic computations. While modern shells offer more integrated ways to perform calculations, expr remains valuable for writing portable shell scripts that need to work across a wide range of Unix environments, especially when dealing with certain types of string pattern matching.

SEE ALSO

bc(1), test(1), bash(1), awk(1), dc(1)

Copied to clipboard