The modulo operator is an essential part of shell scripting that allows you to determine whether a number is divisible by another and find the remainder. Understanding how to effectively use this operator can help you automate tasks that involve numerical conditions, such as validating inputs, looping, or performing repetitive calculations. In this guide, we will walk you through how to use the modulo operator in shell scripts , with detailed examples and solutions to common issues.

Command line execution

Terminalbash
find the remainder. Understanding how to effectively use this operator can help you automate tasks that involve numerical conditions, such as validating inputs, looping, or performing repetitive calculations. In this guide, we will walk you through how to use the  modulo operator in shell scripts , with detailed examples and solutions to common issues. By the end of this article, you will be able to create a  shell script  that uses the modulo operator to determine divisibility and print remainders. What is the Modulo Operator in Shell Scripting? The  modulo operator ( % )  is used to determine the remainder of a division operation. In shell scripting, it is especially useful for checking if one number is evenly divisible by another. Example : The result of  10 % 3  is  1 , because  10  divided by  3  leaves a remainder of  1 . Common Use Cases : Check Divisibility : Determine if a number is even or odd. Loop Control : Execute certain tasks only when a specific condition is met. Data Validation : Ensure numbers meet specific criteria, such as being divisible by a specific value. How to Use the Modulo Operator in Shell Script In  Bash , arithmetic operations can be performed using  $((...))  syntax. The modulo operator can be used in a similar way to find the remainder. Example Script to Find the Remainder Consider the following script, which takes two numbers as command line arguments and prints whether the first number is divisible by the second: #!/bin/bash # Assigning arguments to variables NUM1=$1 NUM2=$2 # Checking if both arguments are provided if [ $# -ne 2 ]; then     echo "Usage: $0 <num1> <num2>"     exit 1 fi # Performing modulo operation to find the remainder REMAINDER=$((NUM1 % NUM2)) # Checking if divisible echo "Remainder when $NUM1 is divided by $NUM2: $REMAINDER" if [ $REMAINDER -eq 0 ]; then     echo "$NUM1 is divisible by $NUM2." else     echo "$NUM1 is not divisible by $NUM2." fi Explanation : NUM1=$1  and  NUM2=$2 : Assign command line arguments to variables. if [ $# -ne 2 ] : Check if exactly two arguments are provided. REMAINDER=$((NUM1 % NUM2)) : Calculate the remainder when  NUM1  is divided by  NUM2 . Conditional Statement : Print whether  NUM1  is divisible by  NUM2  based on the remainder. Running the Script To execute this script, save it as  modulo_check.sh  and make it executable: chmod +x modulo_check.sh Run the script with two numbers as arguments: ./modulo_check.sh 10 3 Output : Remainder when 10 is divided by 3: 1 10 is not divisible by 3. Understanding Divisibility with the Modulo Operator If the  remainder  is  0 , it means the first number is  divisible  by the second. If the remainder is anything other than  0 , it indicates the number is  not divisible . For example: 15 % 5  results in  0 , meaning  15  is divisible by  5 . 17 % 4  results in  1 , meaning  17  is not evenly divisible by  4 . Common Issues and Solutions 1. Division by Zero Attempting to divide by zero will result in an error. Solution : Add a condition to check if the divisor ( NUM2 ) is zero before performing the modulo operation. if [ $NUM2 -eq 0 ]; then     echo "Error: Division by zero is not allowed."     exit 1 fi 2. Handling Non-Numeric Inputs If non-numeric values are provided as arguments, the script will fail. Solution : Use regex to validate that both inputs are numeric before proceeding. if ! [[ $NUM1 =~ ^[0-9]+$ ]] || ! [[ $NUM2 =~ ^[0-9]+$ ]]; then     echo "Error: Both arguments must be numeric."     exit 1 fi Advanced Techniques for Using the Modulo Operator 1. Checking Even or Odd Numbers The modulo operator is often used to determine whether a number is even or odd. Example : #!/bin/bash NUMBER=$1 if [ $((NUMBER % 2)) -eq 0 ]; then     echo "$NUMBER is even." else     echo "$NUMBER is odd." fi Explanation : If  NUMBER % 2  equals  0 , the number is even; otherwise, it is odd. Best Practices for Using the Modulo Operator in Shell Scripts Validate Input : Always validate that inputs are numeric to avoid unexpected errors. Check for Division by Zero : Ensure the divisor is not zero before performing the modulo operation. Use Meaningful Variable Names : Use descriptive variable names to make your script easier to read and understand. Provide Usage Information : If arguments are missing, inform the user how to run the script correctly. Example Script with Input Validation #!/bin/bash # Assign arguments to variables NUM1=$1 NUM2=$2 # Validate inputs if [ $# -ne 2 ]; then     echo "Usage: $0 <num1> <num2>"     exit 1 fi if ! [[ $NUM1 =~ ^[0-9]+$ ]] || ! [[ $NUM2 =~ ^[0-9]+$ ]]; then     echo "Error: Both arguments must be numeric."     exit 1 fi if [ $NUM2 -eq 0 ]; then     echo "Error: Division by zero is not allowed."     exit 1 fi # Perform modulo operation REMAINDER=$((NUM1 % NUM2)) echo "Remainder when $NUM1 is divided by $NUM2: $REMAINDER" if [ $REMAINDER -eq 0 ]; then     echo "$NUM1 is divisible by $NUM2." else     echo "$NUM1 is not divisible by $NUM2." fi Conclusion The  modulo operator in shell scripting  is a powerful tool for determining divisibility and finding remainders. Whether you’re writing scripts to validate inputs, manage looping conditions, or simply automate numerical tasks, understanding how to use the modulo operator effectively is crucial. This guide has provided an in-depth look at how to use the modulo operator in shell scripts, common issues, and advanced use cases. Implement these examples in your scripts to make them more versatile and capable of handling a wide range of numerical scenarios.

Risk level: destructive. Review the command before running it.

Implementation details

By the end of this article, you will be able to create a shell script that uses the modulo operator to determine divisibility and print remainders. What is the Modulo Operator in Shell Scripting? The modulo operator ( % ) is used to determine the remainder of a division operation. In shell scripting, it is especially useful for checking if one number is evenly divisible by another. Example : The result of 10 % 3 is 1 , because 10 divided by 3 leaves a remainder of 1 .

Gotchas and common issues

  • Permission checks - verify user access rights and sudo privileges before executing system-level operations.

  • Environment configuration - double-check path variables and dependency versions to prevent runtime failures.

  • Backup safeguards - maintain configuration backups before applying system or database modifications.

Following these steps ensures clean configuration and reliable execution for modulo operator: shell script to find if a number is divisible and print the remainder.