How to make a statement that checks if something is divisible by something else without a remainder (BASH)
Solution 1:
You can use a simpler syntax in Bash than some of the others shown here:
#!/bin/bash
read -p "Enter a number " number # read can output the prompt for you.
if (( $number % 5 == 0 )) # no need for brackets
then
echo "Your number is divisible by 5"
else
echo "Your number is not divisible by 5"
fi
Solution 2:
No bc needed as long as it's integer math (you'll need bc for floating point though): In bash, the (( )) operator works like expr.
As others have pointed out the operation you want is modulo (%).
#!/bin/bash
echo "Enter a number"
read number
if [ $(( $number % 5 )) -eq 0 ] ; then
echo "Your number is divisible by 5"
else
echo "Your number is not divisible by 5"
fi