Bash can handle integer math right inside the shell. With arithmetic expansion, a compact expression such as $((3 + 3)) becomes 6, ready to print or store in a variable. It’s a clear, built-in approach for Linux scripts, without calling an external calculator.

Multiple Choice

How do you perform arithmetic in Bash so that 3 + 3 prints 6?

Arithmetic expansion in Bash is performed with the syntax $(( ... )). The expression inside is evaluated as integer arithmetic by the shell, and the result replaces the expansion. So echo $((3 + 3)) asks Bash to compute 3 + 3 and substitute 6, which then gets printed. This method is built-in, fast, and works in Bash (and POSIX shells), and spaces around operators are optional, so you can also write echo $((3+3)). The other options rely on older or external mechanisms: the deprecated $[ ... ] form, or the external expr program, or the let builtin in a way that requires additional steps to print the result. The straightforward and idiomatic way to get 6 is the arithmetic expansion shown.

Bash can do simple arithmetic without calling an external program. Write echo $((3 + 3)), and the shell calculates the expression before echo runs. The result is 6. This small feature comes in handy in Linux command-line work, where a quick calculation can help with counters, file sizes, loop limits, or a script’s exit status.

The syntax is called arithmetic expansion: $(( ... )). Bash evaluates the integer expression inside the parentheses, then replaces the whole expansion with its result. Think of it as a tiny calculator built into the shell—one that’s already sitting at your prompt.

Bash does the calculation before printing

When Bash reads this command:


echo $((3 + 3))

it first evaluates 3 + 3. That produces the value 6, so the command Bash effectively runs is:


echo 6

The output is:


6

Spaces around the + are optional. These two commands behave the same way:


echo $((3 + 3))

echo $((3+3))

Still, spaces often make expressions easier to read, especially once they grow beyond a couple of numbers.

Arithmetic expansion works with variables, too. In Bash, a variable can be used by name inside the expression:


count=3

echo $((count + 3))

Bash prints 6. You don’t need to write $count inside the arithmetic expression, though Bash accepts that form as well. Using the bare variable name keeps the expression tidy.

Add, subtract, multiply, and divide

Once the basic form feels familiar, you can use the usual arithmetic operators:


echo $((8 - 2))   # 6

echo $((4 * 3))   # 12

echo $((20 / 5))  # 4

echo $((7 % 3))   # 1

The % operator returns the remainder after division. Here, dividing 7 by 3 leaves a remainder of 1. That makes it useful for checks such as finding out whether a number is even:


number=14

if (( number % 2 == 0 )); then

echo "even"

fi

This example uses another Bash feature: the (( ... )) arithmetic command. It evaluates a numeric expression as a condition, so the if body runs when the expression is true. The expression number % 2 == 0 is true when the remainder is zero.

Integer division is worth noting. In Bash arithmetic, 7 / 2 evaluates to 3, not 3.5, because the result is an integer. For decimal calculations, use a tool designed for that job, such as bc:


echo "scale=2; 7 / 2" | bc

That prints 3.50 when bc is available. Bash is a handy calculator for whole numbers, but it isn’t a general-purpose decimal math package.

Variables and counters in shell scripts

Arithmetic expansion becomes especially useful when a script needs to update a value. For example:


count=0

count=$((count + 1))

echo "$count"

The output is 1. The shell reads the old value of count, adds one, and assigns the result back to the variable. You can write the update more briefly with the += operator:


count=0

count=$((count + 1))

count=$((count + 1))

echo "$count"

Or, in Bash, use an arithmetic command:


count=0

((count++))

echo "$count"

These forms are common in loops and scripts that process a series of files or events. For instance, a counter can track how many items a script has handled. It’s a small detail, but it can make command-line output much easier to understand.

Bash also lets you use arithmetic in loop conditions:


for ((i = 1; i <= 3; i++)); do

echo "$i"

done

This prints 1, 2, then 3. The loop has three parts: an initial value, a condition, and an update. It resembles a for loop in languages such as C, so it may look familiar if you’ve seen those before.

How it compares with older approaches

You may come across other ways to calculate in shell examples. The expr command, for instance, performs arithmetic as a separate program:


expr 3 + 3

It prints 6, but it has stricter spacing rules and launches an external command. Arithmetic expansion is more direct when you’re already working in Bash:


echo $((3 + 3))

Another form is let, a Bash builtin:


let "result = 3 + 3"

echo "$result"

That works, although arithmetic expansion often makes the calculation and the printed value clearer in one line. You might also see $[3 + 3] in older scripts. Bash supported that form, but it is deprecated; prefer $((3 + 3)) in new code.

The key distinction is simple: arithmetic expansion produces a value that can be placed inside a command, while (( ... )) evaluates arithmetic as a command and reports success or failure based on the result. For example:


result=$((3 + 3))

echo "$result"

Here, the calculation is stored first, then printed. That two-step style is useful when the value will be used more than once.

A few details that can surprise you

Bash arithmetic uses integers, and it treats leading zeroes in some number forms as octal. That means a value like 010 may be read as octal for eight, rather than decimal ten. If a number comes from input and should be decimal, you can make the base explicit with 10#:


value=010

echo $((10#$value))

This prints 10. Small quirks like this matter when a script reads values that look numeric but may have been formatted for people.

Arithmetic expressions can also use comparison and logical operators:


age=21

if (( age >= 18 )); then

echo "adult"

fi

Inside (( ... )), you don’t need $ before age. Bash treats variable names as values in that context. The same expression written with arithmetic expansion would yield 1 when true and 0 when false:


echo $((age >= 18))

One caution: arithmetic evaluation is not a substitute for careful input handling. If a script reads a value from a user, a file, or a network response, validate that it’s a number before using it in calculations. Bash arithmetic has its own rules, and untrusted input should never be treated as harmless text just because it appears in a math expression.

The short version

For a quick calculation, use arithmetic expansion:


echo $((3 + 3))

Bash evaluates the expression and prints 6. The same syntax works with variables and larger integer expressions, making it a natural fit for everyday shell scripts. Use (( ... )) when you want arithmetic to control a condition or update a counter, and reach for a tool such as bc when you need decimal math.

It’s a small piece of Bash, but a useful one. Once you recognize $(( ... )), arithmetic in a shell script stops looking like a separate task and becomes just another part of the command.