Cancel

Arithmetic Operators in Bash

Arithmetic operators are simple calculations, like addition, subtraction, multiplication, and etc. This is simple and straightforward topic, I believe no need to explain here. Because we all did calculations in our school.

Your expression goes inside the 2 round brackets with the dollar prefix.

  • $( command ) means shell execution.
  • $(( expression )) means do some calculation.

Here are few examples for you to understand more.

1
2
3
4
5
6
7
8
9
10
11
12
echo $(( expression )) # dummy expression
echo $(( 3 + 2 )) # simple expression, without variable

# calculation with variable
number=3
echo $((number += 2)) # addition
echo $((number -= 2)) # subtract
echo $((number *= 2)) # multiplication
echo $((number /= 2)) # division
echo $((number %= 2)) # modulus or remainder
echo $((number++)) # increament
echo $((number--)) # descrement