arithmetic expansion
Evaluating whole-number maths inside $(( )) and replacing it with the result; (( )) on its own evaluates without printing anything.
In more detail
Variables inside do not need a dollar sign, and the usual operators work, including % for remainder and ++ to add one. The bare (( )) form returns success when the result is non-zero, so it doubles as a numeric test in if and while. Only integers are supported; use bc for decimals.
Examples
n=$(( n + 1 ))(( count++ ))Related terms
- integer arithmetic — Maths on whole numbers only, which is all bash can do itself: 7 / 2 gives 3, with the remainder dropped.
- let builtin — The bash builtin that evaluates arithmetic given as arguments, as in let "total = a + b".
- bc — A calculator program with arbitrary precision that does the decimal maths bash itself cannot.
Taught in
- Which Shell Am I In, And Getting Bash Five — Bash Scripting & Command Line Automation
- What Bash Does To A Line Before It Runs It — Bash Scripting & Command Line Automation
- The Field Separator, And Reading Lines Safely — Bash Scripting & Command Line Automation
- Arithmetic: Double Parentheses, let And bc — Bash Scripting & Command Line Automation
- Loops: for, while, until, break, continue — Bash Scripting & Command Line Automation
- Functions, local And declare — Bash Scripting & Command Line Automation
- Option Parsing With getopts — Bash Scripting & Command Line Automation
- POSIX sh Versus Bash — Bash Scripting & Command Line Automation