Bash (Scripting) - Division

by
Jeremy Canfield |
Updated: April 23 2023
| Bash (Scripting) articles
Here is a basic example of how to divide whole numbers in a Bash Shell script.
#!/bin/bash
echo $((4 / 2))
Or like this.
#!/bin/bash
sum=$((4 / 2))
echo $sum
Or like this.
#!/bin/bash
x=4
y=2
sum=$((x / y))
echo $sum
Which should return the following.
2
The following can be used when dealing with non-whole numbers (decimals), piping to the bc command.
#!/bin/bash
x="2.47"
y="1.178"
sum=$(echo "$x / $y" | bc)
echo $sum
Or using the awk command.
#!/bin/bash
x="2.47"
y="1.178"
sum=$(awk "BEGIN{print $x / $y}")
echo $sum
Did you find this article helpful?
If so, consider buying me a coffee over at