Bash 5 reference · Shell Builtins and Syntax
$(( )) arithmetic (Bash syntax)
Evaluate integer arithmetic and comparisons with $(( )) and (( )); Bash has no floating point, so use awk or bc.
Bash 5
Syntax
$(( EXPRESSION )) (( EXPRESSION ))
Parameters
| Parameter | Description |
|---|---|
+ - * / % ** |
Operators; / truncates. |
++ += -= |
Increment and compound assignment. |
(( a > b )) |
Comparison as a condition. |
Examples
a=17 b=5
echo "$(( a / b )) rem $(( a % b )), power $(( 2 ** 10 ))"
(( a > b )) && echo 'a is bigger'
Output
3 rem 2, power 1024 a is bigger
Run this on your computer — see “Get Started” for setup.
total=0
for n in 4 8 15; do (( total += n )); done
echo "total $total, avg $(awk -v t=$total 'BEGIN { printf "%.2f", t / 3 }')"
Output
total 27, avg 9.00
Run this on your computer — see “Get Started” for setup.
Related
${...} expansion· Defaults, length, substrings, prefix and suffix removal, search and replace, and case conversion on variables.- Bash Arithmetic · tutorial