Command Substitution & Arithmetic in Bash

From the Bash Scripting cheat sheet · Substitution & Arithmetic · verified Jul 2026

Command Substitution & Arithmetic

$(...) and $((...)).

bash
# Quick Reference
files="$(ls | wc -l)"     # capture command output
sum=$(( 3 + 4 ))          # arithmetic -> 7
(( count++ ))             # arithmetic evaluation
💡 $(( )) evaluates integer math; inside it, variables need no $ prefix.
⚡ Bash arithmetic is integer-only - use bc or awk for floating-point.
📌 (( expr )) returns success/failure by the result, so it chains with && and ||.
🟢 Prefer $(cmd) over backticks - it nests cleanly: $(dirname "$(pwd)").
arithmeticsubstitution
Back to the full Bash Scripting cheat sheet