Bash Script to Divide Two Numbers with Output
Use Bash arithmetic expansion with
result=$((num1 / num2)) to divide two numbers, ensuring num2 is not zero to avoid errors.Examples
Inputnum1=10, num2=2
OutputResult: 5
Inputnum1=15, num2=4
OutputResult: 3
Inputnum1=7, num2=0
OutputError: Division by zero is not allowed.
How to Think About It
To divide two numbers in Bash, first get the two numbers as input. Then check if the divisor is zero to avoid errors. If not zero, use Bash's arithmetic expansion with
$(( )) to perform integer division and print the result.Algorithm
1
Get the first number as input.2
Get the second number as input.3
Check if the second number is zero.4
If zero, print an error message and stop.5
Otherwise, divide the first number by the second using arithmetic expansion.6
Print the division result.Code
bash
#!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 if [ "$num2" -eq 0 ]; then echo "Error: Division by zero is not allowed." exit 1 fi result=$((num1 / num2)) echo "Result: $result"
Output
Enter first number: 10
Enter second number: 2
Result: 5
Dry Run
Let's trace dividing 10 by 2 through the code
1
Input numbers
num1=10, num2=2
2
Check divisor
num2 is 2, not zero, continue
3
Calculate division
result = 10 / 2 = 5
| Step | Variable | Value |
|---|---|---|
| 1 | num1 | 10 |
| 1 | num2 | 2 |
| 3 | result | 5 |
Why This Works
Step 1: Input reading
The script uses read to get two numbers from the user.
Step 2: Division by zero check
It checks if the divisor is zero with [ "$num2" -eq 0 ] to prevent errors.
Step 3: Perform division
The division uses Bash arithmetic expansion $((num1 / num2)) which does integer division.
Step 4: Output result
Finally, it prints the result with echo.
Alternative Approaches
Using bc for floating point division
bash
#!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 if [ "$num2" = "0" ]; then echo "Error: Division by zero is not allowed." exit 1 fi result=$(echo "scale=2; $num1 / $num2" | bc) echo "Result: $result"
This method allows decimal division but requires the bc utility.
Using awk for division
bash
#!/bin/bash read -p "Enter first number: " num1 read -p "Enter second number: " num2 if [ "$num2" = "0" ]; then echo "Error: Division by zero is not allowed." exit 1 fi result=$(awk "BEGIN {print $num1/$num2}") echo "Result: $result"
This method also supports floating point division using awk.
Complexity: O(1) time, O(1) space
Time Complexity
The division operation and checks run in constant time since they involve only simple arithmetic and comparisons.
Space Complexity
The script uses a fixed amount of memory for variables, so space complexity is constant.
Which Approach is Fastest?
Using Bash arithmetic expansion is fastest for integer division; using bc or awk adds overhead but supports decimals.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bash arithmetic expansion | O(1) | O(1) | Fast integer division |
| bc utility | O(1) | O(1) | Floating point division with decimals |
| awk utility | O(1) | O(1) | Floating point division with decimals |
Always check if the divisor is zero before dividing to avoid runtime errors.
Trying to divide by zero without checking causes the script to fail or produce errors.