Bash arithmetic operators in shell scripts

Bash arithmetic operators are used to perform integer calculations in shell scripts. You can add, subtract, multiply, divide, find remainders, raise powers, and update numeric variables directly inside Bash.

The preferred syntax for most Bash arithmetic is $(( expression )). Bash arithmetic expansion works with integers, so $((15 / 8)) returns 1, not 1.875. For decimal arithmetic, use a tool such as bc or awk.

Bash arithmetic operators quick reference table

OperatorOperationExampleResult
+Addition$((15 + 8))23
-Subtraction$((15 - 8))7
*Multiplication$((15 * 8))120
/Integer division$((15 / 8))1
%Modulo or remainder$((15 % 8))7
**Exponentiation$((15 ** 8))2562890625
+=Add and assign((x += 5))Updates x
-=Subtract and assign((x -= 5))Updates x
*=Multiply and assign((x *= 5))Updates x
/=Divide and assign((x /= 5))Updates x
%=Modulo and assign((x %= 5))Updates x

Addition operator (+) in Bash

The addition operator returns the sum of two integer operands.

</>
Copy
#!/bin/bash

x=$(( 15 + 8 ))
echo $x

Output

23

Subtraction operator (-) in Bash

The subtraction operator subtracts the second operand from the first operand.

</>
Copy
#!/bin/bash

x=$(( 15 - 8 ))
echo $x

Output

7

Multiplication operator (*) in Bash

The multiplication operator returns the product of two operands. In $(( )), the asterisk can be used directly.

</>
Copy
#!/bin/bash

x=$(( 15 * 8 ))
echo $x

Output

120

When using the older expr command, escape the multiplication symbol because the shell may treat * as a filename wildcard.

</>
Copy
x=$(expr 15 \* 8)

Division operator (/) and integer quotient in Bash

The division operator returns the integer quotient. Bash removes the fractional part in arithmetic expansion.

</>
Copy
#!/bin/bash

x=$(( 15 / 8 ))
echo $x

Output

1

Exponentiation operator (**) in Bash

The exponentiation operator raises the first operand to the power of the second operand. For example, 15 ** 8 means 15 raised to the 8th power.

</>
Copy
#!/bin/bash

x=$(( 15 ** 8 ))
echo $x

Output

2562890625

Modulo operator (%) for remainders in Bash

The modulo operator returns the remainder after division. It is commonly used to check whether a number is odd or even, rotate indexes, or run an action every nth iteration.

</>
Copy
#!/bin/bash

x=$(( 15 % 8 ))
echo $x

Output

7

Assignment arithmetic operators in Bash variables

Assignment arithmetic operators calculate a new value and store it back in the same variable. They are useful for counters, totals, and repeated updates.

</>
Copy
#!/bin/bash

x=12

(( x += 5 ))
echo "after += $x"

(( x -= 3 ))
echo "after -= $x"

(( x *= 2 ))
echo "after *= $x"

(( x /= 4 ))
echo "after /= $x"

(( x %= 5 ))
echo "after %= $x"

Output

after += 17
after -= 14
after *= 28
after /= 7
after %= 2

Increment and decrement operators in Bash loops

Bash also supports ++ and -- in arithmetic evaluation. These operators increase or decrease a variable by one.

</>
Copy
#!/bin/bash

count=0

while (( count < 3 ))
do
  echo "count=$count"
  (( count++ ))
done

Output

count=0
count=1
count=2

Different ways to compute arithmetic operations in Bash

Using $(( )) for Bash arithmetic expansion

Use $(( expression )) when you need the calculated result as a value. This is the clearest form for assigning arithmetic results or printing them.

</>
Copy
#!/bin/bash

x=10
y=3

sum=$(( x + y ))
product=$(( x * y ))

echo "$sum"
echo "$product"

Using (( )) to update Bash numeric variables

Use (( expression )) when the expression updates a variable or is used as a numeric condition. You do not need $ before variable names inside this syntax.

</>
Copy
#!/bin/bash

total=0

for n in 2 4 6
do
  (( total += n ))
done

echo "$total"

Output

12

Using let and expr in older Bash arithmetic scripts

The let built-in and the expr command are still found in older shell scripts. Quote let expressions when they contain spaces. With expr, keep spaces between operands and operators.

</>
Copy
#!/bin/bash

a=5
b=7

let "c = a + b"
echo "$c"

d=$(expr "$a" + "$b")
echo "$d"

Decimal arithmetic with bc in Bash scripts

Use bc when a Bash script needs decimal output. The scale value controls the number of digits after the decimal point.

</>
Copy
#!/bin/bash

echo "scale=2; 15 / 8" | bc

Output

1.87

Common Bash arithmetic operator mistakes

  • Expecting decimal results: Bash arithmetic expansion is integer-only.
  • Forgetting spaces with expr: use expr 5 + 2, not expr 5+2.
  • Forgetting to escape * with expr: use expr 5 \* 2.
  • Adding spaces around assignment: use x=5, not x = 5.
  • Dividing by zero: check the denominator before using / or /=.

Bash arithmetic operators FAQ

Does Bash arithmetic support decimal numbers?

No. Bash arithmetic expansion works with integers. Use bc or awk when a script needs decimal output.

Why does $((15 / 8)) return 1?

It returns 1 because Bash performs integer division and keeps only the quotient. The remainder is available with $((15 % 8)).

Should I use expr or $(( )) for Bash arithmetic?

Use $(( )) for most new Bash scripts. It is easier to read and avoids the quoting and spacing issues common with expr.

How do I increment a variable by one in Bash?

Use ((x++)), ((x += 1)), or x=$((x + 1)). The ((x++)) form is common for loop counters.

Why does multiplication with expr fail in some scripts?

The shell can expand * as a filename wildcard before expr runs. Escape it as \*, quote it, or use $((5 * 2)).

QA checklist for Bash arithmetic operators tutorial

  • Verify that every arithmetic example uses integer results unless the example uses bc.
  • Check that expr multiplication examples escape or quote *.
  • Confirm that assignment operators such as += and /= update the original variable.
  • Make sure loop examples using ++ have a clear stopping condition.
  • Confirm that output blocks are marked with the output class.

Summary of Bash arithmetic operators

In this Bash Tutorial, we have learnt Arithmetic Operators supported by Bash Shell with example for each. In practical scripts, use $(( )) to get a calculated value and (( )) to update numeric variables or test arithmetic conditions.