Bash (Scripting) - Multiplication

by
Jeremy Canfield |
Updated: March 23 2022
| Bash (Scripting) articles
Here is a basic example of how to multiply whole numbers in a Bash Shell script.
#!/bin/bash
echo $((4 * 2))
Which should return the following.
8
Or like this.
#!/bin/bash
sum=$((4 * 2))
echo $sum
Or like this.
#!/bin/bash
x=4
y=2
sum=$((x - y))
echo $sum
decimal
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