Skip to content
Arithmetic
step 1/5

Reading — step 1 of 5

Learn

~3 min readGetting Started

Arithmetic

Bash variables are text — so 2 + 2 is three words, not math. Arithmetic happens only inside a dedicated context, and knowing where those contexts are (and what they can't do) is the whole lesson.

$(( )) — the arithmetic expansion

a=7
b=5
echo "$((a + b))"        # 12
echo "$((a * b))"        # 35
echo "$(( (a + b) * 2 ))" # 24 — parentheses fine, spaces fine

Everything between $(( )) is evaluated as integer math, and the result substitutes into the line as text. Niceties inside the double parens: variables don't need their $ (a + b, not $a + $b — though both work), and the usual operators exist: + - * / %, comparison (<, >=, == — returning 1/0), and even ** for powers.

count=$((count + 1))      # increment — the eternal loop companion
((count++))               # same thing, statement form

Integer only. Really.

echo "$((7 / 2))"        # 3 — truncated, same as C/Go/Rust
echo "$((3.5 + 1))"      # ERROR: syntax error in expression

Bash has no floating point at all. Not "awkward" — absent. The division-truncates rule you know from three other tracks applies, and decimals are a syntax error. When a script genuinely needs decimal math, the idiom is to delegate to a calculator program — remember, bash's superpower is running programs:

echo "3.5 + 1.25" | bc -l        # 4.75 — bc is the classic arbitrary-precision calc
awk 'BEGIN { printf "%.2f\n", 7/2 }'   # awk does floats too, with formatting

For this course's exercises, integer math covers everything — but "bash can't float" is a fact that saves you an hour of confusion someday, so it's stated here in bold: bash arithmetic is integers only.

Where numbers actually come from

In real scripts, numbers arrive as text from commands and files — which is fine, because arithmetic context converts text-that-looks-numeric automatically:

read n                    # n is text: "42"
echo "$((n * 2))"         # 84 — text became number inside $(( ))

One sharp edge for the curious: a variable containing non-numeric text evaluates as 0 inside $(( )) (or errors, depending on content) — garbage in, zero out, silently. Validation (checking input looks like a number before math) belongs to the case/[[ ]] toolkit a chapter ahead; for graded exercises the input format is promised, so trust it for now.

There's also an older arithmetic command you'll see in the wild: expr 7 + 2 — from before $(( )) existed. Recognize it in old scripts; never write it (it forks a process to add two numbers and its syntax is booby-trapped).

Your exercise: Rectangle Area

Read width and height (input format decides one read or two — check the examples, per last lesson), multiply, print:

read w
read h
echo "$((w * h))"

The graded concepts: values must travel into the arithmetic context to be numbers, and the result travels out as text for echo. That round-trip — text → number → text — is bash arithmetic in one line, and it's the same line you'll use for counters, totals, and indices for the rest of the course.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…