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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

$(( EXPRESSION ))   (( EXPRESSION ))

Parameters

ParameterDescription
+ - * / % ** Operators; / truncates.
++ += -= Increment and compound assignment.
(( a > b )) Comparison as a condition.

Examples

Integer maths
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.

Accumulate, with awk for decimals
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.

Was this page helpful?