Linux expr Command: Evaluate Math and Strings
The expr command is a fundamental Unix and Linux utility
designed to evaluate expressions and return the corresponding result to
standard output. This article provides a straightforward guide to
understanding the role of expr, covering its syntax, how it
performs integer arithmetic, its utility in string manipulation, and how
it handles boolean logic within shell scripts.
What is the expr Command?
The expr command parses and evaluates positional
arguments as expressions. It is a standard POSIX utility, making it
crucial for writing portable shell scripts that run across various
Unix-like environments without relying on shell-specific built-in
features.
A critical syntax requirement of expr is that every
operator and operand must be separated by a space. Missing spaces will
cause the command to output the literal arguments rather than evaluating
them.
Evaluating Arithmetic Expressions
The expr command performs basic integer arithmetic. It
does not support floating-point operations natively.
Supported Arithmetic Operators
- Addition (
+): Adds two integers. - Subtraction (
-): Subtracts the second integer from the first. - Multiplication (
*): Multiplies two integers. Because the asterisk is a shell wildcard, it must be escaped with a backslash (\*). - Division (
/): Performs integer division, discarding the remainder. - Modulus (
%): Computes the remainder of an integer division.
Examples
# Addition
expr 10 + 5
# Output: 15
# Subtraction
expr 20 - 8
# Output: 12
# Multiplication (requires escaping)
expr 6 \* 7
# Output: 42
# Division
expr 20 / 3
# Output: 6
# Modulus
expr 20 % 3
# Output: 2Evaluating String Expressions
Beyond numeric calculation, expr provides built-in
operations for analyzing and manipulating strings.
Calculating String Length
To find the number of characters in a string:
expr length "Hello World"
# Output: 11Extracting Substrings
The substr keyword extracts characters starting from a
specified 1-based index for a set length:
# Syntax: expr substr STRING POSITION LENGTH
expr substr "LinuxOS" 1 5
# Output: LinuxFinding Character Index
The index keyword returns the position of the first
matching character from a set of characters:
# Find the first occurrence of 'x' or 'u'
expr index "LinuxOS" "xu"
# Output: 4Regular Expression Matching
The colon (:) operator performs regular expression
matching against the start of a string. It returns either the number of
matching characters or the matched capture group:
# Match from the start of the string
expr "sample.txt" : '.*\.txt'
# Output: 10
# Capture a pattern
expr "file_123.log" : '.*_\([0-9]*\)'
# Output: 123Logical and Relational Operations
expr can evaluate relational conditions, returning
1 for true and 0 for false. Operators such as
<, >, <=, and
>= must be escaped to prevent shell redirection.
# Equality check
expr 10 = 10
# Output: 1
# Greater than check (escaped)
expr 15 \> 20
# Output: 0
# Logical OR (returns first non-empty/non-zero argument, else 0)
expr "" \| "fallback"
# Output: fallbackWhen to Use expr
While modern shells like Bash provide internal arithmetic expansion
such as $(( ... )), the expr command remains
widely used when writing scripts for POSIX-compliant environments like
standard sh, or when working on legacy UNIX systems where
modern bashisms are not supported. Its ability to mix string parsing,
pattern matching, and arithmetic in a single standardized binary makes
it a versatile utility for Linux systems administrators.