Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux expr command

Reference for the Linux expr command: integer arithmetic, comparisons, string length, substr, index and regex matching, with quoting rules and exit statuses.

expr evaluates an expression made of its arguments and prints the result. It does integer arithmetic, comparisons, boolean logic and a few string operations, including a basic regular-expression match. It predates shell arithmetic: in old Bourne scripts expr was the only way to add two numbers.

In Bash you will usually prefer $(( )) for arithmetic and [[ =~ ]] or ${var:offset:length} for strings. expr is still useful in portable sh scripts and for its regex match, which prints the captured text.

Every operator and operand is a separate argument, so spaces around operators are required, and characters that the shell treats specially must be escaped.

Terminal
expr 7 + 5

Try it

Bash Arithmetic and comparisons

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash String operations

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
expr EXPRESSION
expr OPTION

Operators, from lowest to highest precedence:

Operator Result
A | B A if it is neither empty nor 0, otherwise B.
A \& B A if both A and B are non-empty and not 0, otherwise 0.
A = B, !=, \<, \<=, \>, \>= Comparison, printing 1 or 0. Numeric if both sides are integers, otherwise string comparison.
A + B, A - B Addition and subtraction.
A \* B, A / B, A % B Multiplication, integer division, remainder.
STRING : REGEX, match STRING REGEX Anchored basic regex match; prints the match length or the first \( \) group.
substr STRING POS LENGTH Substring, positions counted from 1.
index STRING CHARS Position of the first character of STRING that is in CHARS, or 0.
length STRING Length of the string.
( EXPR ) Grouping; write it as \( EXPR \) in the shell.

Returns (exit status): 0 if the result is neither null nor 0; 1 if the result is null or 0; 2 for a syntax error; 3 for an error such as division by zero.

Usage notes

  • Escape *, (, ), <, >, | and &. Unescaped, the shell expands or redirects them. Quote operands too: expr "$a" + 1 survives a value with spaces.
  • A zero result exits 1, which can trip set -e. expr 1 - 1 is a "failure". Use $(( )) or || true in strict scripts.
  • Integers only. There are no decimals. expr 1.5 + 1 fails with non-integer argument; use awk or bc for fractions.
  • Operands that look like operators confuse it. A variable that holds a word such as length or match can be misread as a keyword, and -- is not supported. Prefix both sides with x for string comparisons (expr "x$a" = "x$b") or use [ ] instead.
  • The regex is basic and anchored. expr "$s" : 'world' is 0 because matching starts at the beginning; write '.*world'. Groups use \( \), and +, ?, | are not special unless escaped (GNU extension).
  • Arithmetic in Bash: $((n + 1)), ((n++)) and let are faster because they do not start a process. Each expr call is a fork and exec.
  • index and length, substr are GNU extensions beyond POSIX, which defines only the operators and :.
  • Large numbers: GNU expr uses arbitrary precision when built with GMP; others overflow at 64 bits.