Integer math and division
Bash arithmetic works with integers, not decimal fractions. Use $(( expression )) to substitute a calculated value into a command or assignment. Inside it, variable names can be written without $; for example, total=$(( price * quantity )). The operators +, -, *, / and % perform addition, subtraction, multiplication, division and remainder. Integer division truncates toward zero: 7 / 3 is 2 and -7 / 3 is -2. The remainder is the amount left after subtracting quotient times divisor. Division by zero is an error. Bash uses fixed-width integers without overflow checks; use small bounded inputs and never assume unlimited precision. Unlike printf, arithmetic expansion calculates a value rather than formatting it. For integer operands, the remainder has the sign of the dividend when nonzero: -7 % 3 is -1. In a noninteractive shell, a division-by-zero error during arithmetic expansion can terminate execution before the next command.
Practical examples
Run each example separately in Bash 5.2 in a fresh empty practice directory. Tests use default shell options and C locale; the fractional lesson requires awk. Output is shown separately; do not enter it as code.
Calculate an order total
packs=4items_per_pack=6printf '%s\n' "$(( packs * items_per_pack ))"Expected stdout:
24Multiplication combines the two integer values.
Full batches and leftovers
items=23size=5printf '%s\n' "$(( items / size ))" "$(( items % size ))"Expected stdout:
43Four complete batches use 20 items; three remain.
Negative division
Expected stdout:
-2-1The quotient truncates toward zero; -7 = (-2)*3 + (-1).
Common mistakes
$(( 7 / 2 )) is 3, not 3.5. Do not feed fractional strings into Bash arithmetic or divide by an unchecked zero divisor.
Practice
For 29 items and boxes of 6, print the number of full boxes and leftover items. Expected: 4 and 5, on separate lines. Repeat with 30 items: 5 and 0.
Next: Arithmetic commands and status