Linux expr Guide - ryzendew/Linux-Tips-and-Tricks GitHub Wiki
Linux expr Guide
Complete beginner-friendly guide to expr on Linux, covering Arch Linux, CachyOS, and other distributions including arithmetic operations, string operations, and expression evaluation.
Table of Contents
expr Basics
Arithmetic
Basic usage:
# Addition
expr 5 + 3
# Output: 8
Note on Spaces
Important:
# Spaces required around operators
expr 5 + 3 # Correct
expr 5+3 # Wrong (no spaces)
# Must have spaces around +, -, *, etc.
Arithmetic Operations
Basic Math
Operations:
# Addition
expr 10 + 5
# Subtraction
expr 10 - 5
# Multiplication (escape *)
expr 10 \* 5
# Division
expr 10 / 5
# Modulo
expr 10 % 3
String Operations
String Length
Length:
# String length
expr length "Hello World"
# Output: 11
Substring
Extract substring:
# Substring
expr substr "Hello World" 1 5
# substr string start length
# Output: Hello
Pattern Matching
Match Pattern
Pattern match:
# Match pattern
expr "Hello" : ".*"
# : = match operator
# Returns length if match
Extract Match
Get match:
# Extract matched part
expr "Hello World" : "\(.*\) World"
# \( \) = capture group
# Output: Hello
Troubleshooting
expr Not Found
Check installation:
# expr is part of coreutils
# Usually pre-installed
# Check expr
which expr
Summary
This guide covered expr usage, arithmetic, string operations, and expression evaluation for Arch Linux, CachyOS, and other distributions.
Next Steps
- Bash Scripting Guide - Scripting basics
- bc Guide - Calculator
- awk Guide - Advanced text processing
- expr Documentation:
man expr
This guide covers Arch Linux, CachyOS, and other Linux distributions. For distribution-specific details, refer to your distribution's documentation.