HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to BashNumbers & ArithmeticStringsArraysConditions (if)Loops (for / while)Functions
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Arithmetic
✍️ Text
📋 Array
🔁 Condition & Loop
🧩 Functions & Pipes
Bash›Numbers & Arithmetic

Numbers & Arithmetic

Math operations and command substitution $( ) with $(( )).

Simple enough for a 5-year-old to understand

Calculate with $(( ))

Bash works with text by default; $(( )) is used for number operations.

Example code
1
echo $((3 + 4))
Addition: 7
CodeAddition: 7
2
echo $((7 % 2))
Remainder: 1
CodeRemainder: 1
3
echo $((2 * 5))
Multiplication: 10
CodeMultiplication: 10
Code
💡 Hint: Without $(( )), "3 + 4" remains plain text.

🎯 Exercise & Practice

Test what you've learned — answer, write code, check instantly. (1 left)

1.Print the remainder of the division of 23 by 4.

🎯 Expected output: 3

Loading editor…

About this topic

Variables in Bash are TEXT by default. Even when you assign a number, a text made of digits is actually stored. To do arithmetic, you need to tell the shell "this is a calculation"; the way to do that is double parenthesis syntax.

The practical consequence of this design is that the plus sign does not add numbers automatically. Writing two variables side by side neither concatenates the text nor calculates — the shell thinks they are commands and arguments. This is the most common source of errors for beginners.

Bash only does INTEGER arithmetic. An external tool is needed for decimal calculations. This limitation matches Bash's purpose: shell scripts do counting, comparison, and index math, not scientific computing.

What you will learn

  • Assigning numbers to a variable
  • That there must be no spaces in assignment
  • Doing arithmetic with double parentheses
  • That Bash does integer arithmetic
  • What is needed for decimal calculations

When to use?

For counters, indices, and simple addition, double parenthesis syntax is enough and fast because it doesn't call an external program.

If decimal calculations are needed, an external calculation tool is called; when this is truly needed, using Python instead of Bash is usually more accurate.

Common mistakes

Wrong

count = 5
echo "$count"

Because there is a space around the equals sign, the shell thinks this is a command call and gives a "count: command not found" error. There can be NO space in assignment in Bash; this rule stems from the shell reading every line as a command and is the most common Bash mistake.

Correct

count=5
echo "$count"

Wrong

a=5
b=3
echo "$a + $b"

The plus sign inside double quotes does not calculate; it prints "5 + 3" literally to the screen. There is no error message, only the wrong output. You need to explicitly tell the shell to calculate for arithmetic.

Correct

a=5
b=3
echo "$((a + b))"

Wrong

a=7
b=2
echo "$((a / b))"

Bash only does integer arithmetic: the result becomes 3 instead of 3.5, and the decimal part is dropped without a warning. If a decimal result is needed, using an external calculation tool is mandatory; the shell itself cannot do this.

Correct

a=7
b=2
echo "scale=2; $a / $b" | bc

Frequently asked questions

Why can't there be spaces in an assignment?

Because Bash parses every line into commands and arguments first. If you leave a space, the first word is considered a command name. This rule is consistent: for the same reason, spaces INSIDE square brackets are mandatory, because square brackets are also a command.

Can I define a variable as a number?

You can give an integer attribute with declare; then assigning text to the variable produces zero and arithmetic works without parentheses too. It is not widely used, but it helps catch typos early in counters.

How many ways are there to increment a counter?

There are a few ways and they all work: the increment operator inside double parentheses, compound assignment, or the let command. Double parenthesis syntax is the most readable and fastest; the expr call seen in older scripts is slow because it starts a new process every time.

Summary

There must be no spaces around the equals sign in an assignment.

Double parenthesis syntax is required for arithmetic; the plus sign does not add automatically.

Bash only does integer arithmetic and drops the decimal part.

Decimal calculations require an external tool.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code