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 [--help | --version] | EXPRESSION

PARAMETERS

--help
    Display usage help and exit.

--version
    Output version information and exit.

DESCRIPTION

expr is a standard Unix/Linux command-line tool for evaluating simple mathematical, logical, and string expressions, mainly in shell scripts. It processes expressions passed as arguments, performing operations like arithmetic (+, -, *, /, %), logical OR (|) and AND (&), and string functions such as length calculation, substring extraction, character indexing, and regular expression matching.

Expressions are space-separated sequences of operands and operators. For example, expr 5 + 3 \* 2 computes (5 + 3) * 2 = 16 due to left-to-right evaluation. String matching uses STRING : REGEXP, returning the 1-based starting position of the match or 0 if no match.

Key functions include:
- expr length STRING: returns string length.
- expr substr STRING POS LEN: extracts substring (POS 1-based).
- expr index STRING CHARS: position of first char in CHARS.

Operands must be quoted if containing spaces, variables, or shell metacharacters (e.g., '$var'). expr exits with 0 for non-zero numeric result, 1 for zero, and 2+ for errors. It's POSIX-compliant but verbose; prefer shell $(( )) for arithmetic or [[ ]] for strings in modern scripts.

CAVEATS

All elements must be separated by single spaces. No operator precedence or grouping (left-to-right only). Quote operands with special chars. Arithmetic treats non-numbers as 0. Division by zero or invalid ops cause exit 2. Avoid for complex logic due to verbosity and quoting issues.

KEY OPERATORS

Arithmetic: + - * / %
Logical: | & (short-circuit)
Relational: < <= = != >= > (numeric compare, 1/true or 0/false)

STRING FUNCTIONS

length STRING: length of string
substr STRING POS LEN: substring from POS (1-based)
index STRING CHARS: pos of first matching char
STRING : REGEXP: match start pos (basic regex)

EXAMPLES

expr 10 + 5 \* 2 → 15
expr length "Linux" → 5
expr "hello" : "h.*" → 1
expr match "abc123" "[0-9]*" → 4 (GNU ext)
size=$(expr $width \* $height)

HISTORY

Introduced in Version 7 Unix (1979). Standardized in POSIX.1-2001, updated in POSIX.1-2008. GNU implementation in coreutils package since early 1990s, with minor enhancements for compatibility.

SEE ALSO

test(1), bc(1), awk(1), sed(1)

Copied to clipboard