Shell-script - How to calculate modulo in shell script, How to use mod operator in shell script

How to calculate modulo in shell script


When I was writing a shell script I was having a situation where I want to use mod operator. while using mod orerator you must be very careful, it functions as below on different shell.

If you are using sh shell then use expression as below
 #!/bin/sh
 if [ `expr $n % 5` -eq 0 ]
 then
    # do something
 fi

If you are using bash shell, use expression as below
 #!/bin/bash
 if (( $n % 5 == 0 ))
 then
     # do something
 fi

Find remainder of given numbers
 # echo "174 % 5" | bc

    4

Just to find a number is divisible or not
 if [ `echo "174 % 5" | bc` -eq 0 ]

The topic on Shell-script - How to calculate modulo in shell script is posted by - Guru

Hope you have enjoyed, Shell-script - How to calculate modulo in shell scriptThanks for your time

Tech Bluff