Arithmetic Operations
Learn how arithmetic operations work in Bash.
Arithmetic operations
Let’s start with the arithmetic operations since they are relatively simplest. Programming languages use regular symbols to denote them:
+
addition-
subtraction/
division*
multiplication
There are two more operations that are often used in programming. These are exponentiation and division with remainder.
Let’s suppose that we want to raise the number a
to the power of b
. We can write it as ab. Here, a
is the base and b
is the exponent. If we want to raise two to the power of seven, we write 27. The same operation in Bash looks like this:
2**7
Calculating the remainder of a division is a complex but essential operation in programming. So, we should consider it in detail.
Let’s suppose that we divide one integer by another. We get a fractional number in the result. The division operation produced a remainder in this case.
Here’s an additional example. Let’s suppose we want to divide the number 10 (the dividend) by 3 (the divisor). If we round the result, we will get 3.33333 (the quotient). The remainder of the division equals 1 in this case. To find it, we should multiply the divisor 3 by the integer part of the quotient 3 (the incomplete quotient). Then, we subtract the result from the dividend 10. It gives us the remainder, which equals 1.
Let’s write our calculations in ...