Bash Modulo

In Bash scripting, the **modulo** operation, represented by the % symbol, is used to find the remainder when one number is divided by another. The modulo operation is particularly useful when you need to check divisibility, find even or odd numbers, or perform cyclic calculations.

In this tutorial, we will explore different methods of performing the modulo operation in Bash using arithmetic expansion, the expr command, and practical examples.

For a quick overview of the Arithmetic Operations in Bash, you may refer Bash Arithmetic Operations.


Syntax for Modulo in Bash

The syntax for performing the modulo operation in Bash can vary depending on the method used. Here are the common approaches:

Modulo using Arithmetic Expansion:

</>
Copy
$((a % b))

Modulo using expr:

</>
Copy
expr a % b

Examples of Modulo in Bash

Let’s go through different examples to see how the modulo operation can be performed using these methods.


1. Bash Modulo Using Arithmetic Expansion

Arithmetic expansion is the simplest and most common way to perform the modulo operation in Bash. It uses the $((...)) syntax to evaluate the expression.

example.sh

</>
Copy
#!/bin/bash

# Define two numbers
num1=10
num2=3

# Perform modulo operation
remainder=$((num1 % num2))

# Display the result
echo "The remainder when $num1 is divided by $num2 is $remainder."

Output

Bash Modulo Using Arithmetic Expansion

In this example, the modulo operation is performed using $((num1 % num2)), and the result is stored in the variable remainder.


2. Bash Modulo Using expr

The expr command can also be used to perform the modulo operation. Note that spaces are required around the % operator when using expr.

example.sh

</>
Copy
#!/bin/bash

# Define two numbers
num1=15
num2=4

# Perform modulo operation using expr
remainder=$(expr $num1 % $num2)

# Display the result
echo "The remainder when $num1 is divided by $num2 using expr is $remainder."

Output

Bash Modulo Using expr

In this example, the modulo operation is performed using expr, and the result is stored in the variable remainder.


3. Checking for Even or Odd Numbers Using Modulo

The modulo operation is often used to check whether a number is even or odd. If the remainder when divided by 2 is 0, the number is even; otherwise, it is odd.

example.sh

</>
Copy
#!/bin/bash

# Read a number from the user
echo "Enter a number:"
read number

# Check if the number is even or odd
if [ $((number % 2)) -eq 0 ]; then
    echo "$number is even."
else
    echo "$number is odd."
fi

Output

Checking for Even or Odd Numbers Using Modulo

In this example, the modulo operation is used to determine if the input number is even or odd by checking the remainder when divided by 2.


Best Practices for Using Modulo in Bash

  • Use $((...)) for integer modulo operations as it is efficient and easy to use.
  • Ensure that the divisor is not zero to avoid division by zero errors.
  • Use modulo operations for tasks like checking divisibility, finding even/odd numbers, or cyclic counting.
  • When using expr, remember to include spaces around the % operator.