Skip to content
Learn Netverks

Lesson

Step 20/36 56% through track

arithmetic-bash

Arithmetic in Bash

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
beginner

This lesson

This lesson teaches Arithmetic in Bash: the syntax, patterns, and safety habits you need before advancing in Bash.

Teams still ship Arithmetic in Bash in Bash codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Arithmetic in Bash in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons.

When you can explain the previous lesson's ideas without copying starter code.

Bash math uses $(( )) for integers and bc or awk for decimals. Everything else is string manipulation until you opt into arithmetic context.

Integer math

a=10
b=3
echo $((a + b))
echo $((a / b))
echo $((a % b))

Division is integer-only inside $(( )).

Increment

n=0
((n++))
echo "n=$n"
if (( n > 0 )); then echo "positive"; fi

(( )) is arithmetic test/command—returns exit status like other commands.

Floating point

echo "scale=2; 10/3" | bc

For money or science, consider Python (Python track)—Bash is not a calculator for all numeric types.

Important interview questions and answers

  1. Q: Why $(( ))?
    A: Arithmetic expansion—no external expr command needed for integers.
  2. Q: Integer division of 10/3?
    A: 3 in Bash arithmetic, not 3.333.

Self-check

  1. What syntax adds two variables?
  2. Which tool handles decimal division in the example?

Tip: Use Python or bc for floats—Bash integers only in $(( )).

Interview prep

$(( ))?

Integer arithmetic expansion.

bc for?

Decimal math when Bash integers are insufficient.

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • (( )) vs expr?
  • Integer only?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump