Bootstrap FreeKB - Bash (Scripting) - Multiplication
Bash (Scripting) - Multiplication

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 596fba in the box below so that we can be sure you are a human.