Operations in the Arithmetic Expressions

Learn about the operations we can perform in the arithmetic expression, the priority of operations, and choosing a numeral system.

The operations in arithmetic expression

The following table shows the operations we can perform in arithmetic expressions.

Operation Description Example
Calculations
* Multiplication echo "$((2 * 9)) = 18"
/ Division echo "$((25 / 5)) = 5"
% The remainder of division echo "$((8 % 3)) = 2"
+ Addition echo "$((7 + 3)) = 10"
- Subtraction echo "$((8 - 5)) = 3"
** Exponentiation echo "$((4**3)) = 64"
Bitwise operations
~ Bitwise NOT echo "$((~5)) = -6"
<< Bitwise left shift echo "$((5 << 1)) = 10"
>> Bitwise right shift echo "$((5 >> 1)) = 2"
& Bitwise AND echo "$((5 & 4)) = 4"
| Bitwise OR echo "$((5 | 2)) = 7"
^ Bitwise XOR echo "$((5 ^ 4)) = 1"
Assignments
= Ordinary assignment echo "$((num = 5)) = 5"
*= Multiply and assign the result echo "$((num *= 2)) = 10"
/= Divide and assign the result echo "$((num /= 2)) = 5"
%= Get the remainder of the division and assign it echo "$((num %= 2)) = 1"
+= Add and assign the result echo "$((num += 7)) = 8"
-= Subtract and assign the result echo "$((num -= 3)) = 5"
<<= Do bitwise left shift and assign the result echo "$((num <<= 1)) = 10
>>= Do bitwise right shift and assign the result echo "$((num >>= 2)) = 2"
&= Do bitwise AND and assign the result echo "$((num &= 3)) = 2"
^= Do bitwise XOR and assign the result echo "$((num^=7)) = 5"
|= Do bitwise OR and assign the result echo "$((num |= 7)) = 7"
Comparisons
< Less than ((num < 5)) && echo "The \"num\" variable is less than 5"
> Greater than ((num > 5)) && echo "The \"num\" variable is greater than 3"
<= Less than or equal ((num <= 20)) && echo "The \"num\" variable is less or equal 20"
>= Greater than or equal ((num >= 15)) && echo "The \"num\" variable is greater or equal 15"
== Equal ((num == 3)) && echo "The \"num\" variable is equal to 3"
!= Not equal ((num != 3)) && echo "The \"num\" variable is not equal to 3"
Logical operations
! Logical NOT (( ! num )) && echo "The \"num\" variable is FALSE"
&& Logical AND (( 3 < num && num < 5 )) && echo "The \"num\" variable is greater than 3 but less than 5"
|| Logical OR (( num < 3 || 5 < num )) && echo "The \"num\" variable is less than 3 or greater than 5"
Other operations
num++ Postfix increment echo "$((num++))"
num-- Postfix decrement echo "$((num--))"
++num Prefix increment echo "$((++num))"
--num Prefix decrement echo "$((--num))"
+num Unary plus or multiplication of a number by 1 a=$((+num))"
-num Unary minus or multiplication of a number by -1 a=$((-num))"
CONDITION ? ACTION_1 : ACTION_2 Ternary operator a=$(( b < c ? b : c ))
ACTION_1, ACTION_2 The list of expressions ((a = 4 + 5, b = 16 - 7))
( ACTION_1 ) Grouping of expressions (subexpression) a=$(( (4 + 5) * 2 ))

Bash performs all operations in the arithmetic expression in order of their priorities. The operations with a higher priority come first.

Run the commands discussed in this lesson in the terminal below.

Get hands-on with 1400+ tech skills courses.