1. Overview

The modulo operator can be useful for various tasks in Bash scripting, such as checking if a number is divisible by another number or if a number is even or odd.

In this tutorial, we’ll explore some examples of how we can use the modulo operator in Bash and how we can apply it effectively.

The commands used in this tutorial were tested in Bash shell version 5.1.16.

2. The Modulo Operator

The modulo operator, also known as the remainder operator, is a binary operator that evaluates the remainder of a division between two integers. In Bash, we represent the modulo operator with the % symbol.

For instance, 5 % 3 returns 2 because 5 / 3 is 1 with a remainder of 2.

3. Basic Usage of %

In Bash scripting, we use % basically in two ways:

  • arithmetic expressions
  • assignments

Let’s look at these in the following sections.

3.1. Using % in Arithmetic Expressions

An arithmetic expression is a combination of numbers, variables, operators, and parentheses that evaluates to a single value. We can use the modulo operator in arithmetic expressions to perform calculations that involve the remainder of a division:

$ echo $((10 % 4))
2

In the code snippet above, we use the $((…)) construct to perform an arithmetic expression, which evaluates the expression inside and prints out the result with the echo command. We use the modulo operator with 10 as the dividend and 4 as the divisor, and the result is 2, which is the remainder of 10 / 4.

In addition, we can also use % with more complex arithmetic:

$ echo $(((10 + 5) % 4))
3

In the snippet above, we use parentheses to change the order of operations and avoid errors. We add 10 and 5 first, then use the modulo operator with 4 as the divisor. The result is 3, which is the same as (10 + 5) % 4.

3.2. Using % With Assignment

We can also use % with variables and parameters in arithmetic expressions:

$ n=10
$ m=3
$ echo $((n % m))
1

Here, we declare two variables, n and m, with the values 10 and 3, respectively. We then use % to get the remainder of 3 in 10.

However, we can also assign the result of a modulo operator to a variable using the = operator:

$ x=$(((10 + 5) % 4))
$ echo $x
3

Here, we assign the value of (((10 + 5) % 4)) to variable x, and we print out x using the echo command.

Furthermore, we can use the modulo operator with different types of operands and operators, such as negative numbers, hexadecimal numbers, bitwise operators, or conditional operators:

$ echo $((0x0A % 0x03))
1

In this command, we use the modulo operator with hexadecimal numbers as operands. We use the prefix 0x to indicate that the number is in base 16. The modulo operator then returns the remainder of the division between two integers. The result is 1, which is the remainder of hexadecimal A divided by hexadecimal 3.

4. Applications of %

We can use the modulo operator for various applications in Bash scripting, depending on the problem we want to solve. Let’s look at some of the common applications.

4.1. Checking if a Number Is Even or Odd

One of the simplest applications we can use the modulo operator for is to check if a number is even or odd.

We can do this by using the modulo operator with 2 as the divisor. If the result is 0, then the number is even. Otherwise, it’s odd.

Let’s look at an example to demonstrate this:

$ n=10
$ if [ $((n % 2)) -eq 0 ]; then
>   echo "Number $n is even"
> else
>   echo "Number $n is odd"
> fi
Number 10 is even
$ n=7
$ if [ $((n % 2)) -eq 0 ]; then
>   echo "Number $n is even"
> else
>   echo "Number $n is odd"
> fi
Number 7 is odd

In the code snippet above, we tested for two possibilities: whether the number we declared is even or odd using the if else statement. The if statement executes the commands based on the condition we enclosed inside square brackets [ ]. We then use the square brackets to test the expression and return a status of 0 (true) or 1 (false). Furthermore, we use the construct $((…)) to evaluate the expression inside it and replace it with its value. We then use the -eq operator to compare the two numbers for equality.

After that, we display the value of the variable if it’s even and use the else statement to mark the end of the command for the true branch of the if statement and start the command for the false branch. We then display the result with the echo command.

We also use the fi keyword to show that there are no more commands for either branch of the if statement.

4.2. Checking if a Number Is Divisible by Another

Another application of using the modulo operator is to check if a number is divisible by another number. We can do this by using the modulo operator with the divisor as the second operand. If the result is 0, then the number is divisible by the divisor. Otherwise, it’s not.

Let’s look at an example to demonstrate this:

$ n=15
$ m=3
$ if [ $((n % m)) -eq 0 ]; then
>   echo "Number $n is divisible by $m"
> else
>   echo "Number $n is not divisible by $m"
> fi
Number 15 is divisible by 3
$ n=16
$ m=3
$ if [ $((n % m)) -eq 0 ]; then
>   echo "Number $n is divisible by $m"
> else
>   echo "Number $n is not divisible by $m"
> fi
Number 16 is not divisible by 3

In this example, we use the same logic as before but with different operands. However, we can use this technique to check for divisibility by any number, such as 5, 10, or 100.

4.3. Generating Cyclic Patterns or Sequences

In addition, another application we can use the modulo operator for is to generate cyclic patterns or sequences in Bash scripting. We can do this by using the modulo operator with a fixed number as the divisor and a variable number as the dividend. The result will be a sequence of numbers that repeat after a certain interval:

$ n=1
$ while [ $n -le 10 ]; do
>   echo $((n % 3))
>   n=$((n + 1))
> done
1
2
0
1
2
0
1
2
0
1

In the code snippet above, we use a while loop to iterate over a range of numbers from 1 to 10 and the -le option to compare two numbers for less than or equal to. We use the modulo operator with 3 as the divisor and $n as the dividend. The result is a sequence of numbers between 0 and 2. We can use this technique to generate any cyclic pattern or sequence, such as days of the week, months of the year, or colors of the rainbow.

5. Conclusion

In this article, we’ve learned how we can use the modulo operator in Bash scripting.

Firstly, we discussed the % operator itself. We also looked at how we can use the % operator to perform arithmetic operations and assignments.

Finally, we looked at some typical applications of using the modulo operator, such as checking if a number is odd or even, checking if a number is divisible by another number, and generating cyclic patterns or sequences.