Arithmetic Operators

+ Addition $(( a + b ))
- Subtraction $(( a - b ))
* Multiplication $(( a * b ))
/ Division $(( a / b ))
% Modulus (remainder) $(( a % b ))
** Exponent $(( a ** b ))
++ (pre) Increment (post)

$(( ++a ))

$(( a++ ))

-- (pre) Decrement (post)

$(( --b ))

$(( b-- ))

+= N Increment by constant N $(( a += b ))
-= N Decrement by constant N $(( a -= b ))
*= N Multiply by constant N $(( a *= b ))
/= N Divide by constant N $(( a /= b ))
%= N Remainder of Dividing by constant N $(( a %= b ))

 

*note: pre and post operations can be quite confusing and you may not get the result you expect

  • pre will Increment/Decrement the variable before the operation is carried out
  • post will Increment/Decrement the variable after the operation is carried out

 

#!/bin/bash
a=10; echo 'a =' $a
b=3; echo 'b =' $b
echo '$a + $b =' $(( a + b ))
echo '$a - $b =' $(( a - b ))
echo '$a * $b =' $(( a * b ))
echo '$a / $b =' $(( a / b ))
echo '$a % $b =' $(( a % b ))
echo '$a ** $b =' $(( a ** b ))
echo '++ $a =' $(( ++a ))
echo '$a ++ =' $(( a++ ))
echo '-- $a =' $(( --a ))
echo '$a -- =' $(( a-- ))
echo '$a += $b =' $(( a += b ))
echo '$a -= $b =' $(( a -= b ))
echo '$a *= $b =' $(( a *= b ))
echo '$a /= $b =' $(( a /= b ))
echo '$a %= $b =' $(( a %= b ))