Multiply decimals in a Shell Script

In this article, we would cover how to multiply decimals in a Shell Script. We will take BASH as reference for writing Shell Scripts. In previous article, we have already seen how to Add two numbers in a Shell Script. But, that was specific to integer values.

What if we try to multiply two decimals or a decimal and a whole number (int). In that case, we need to use bc command. We use bc command-line utility as calculator.

We will see how we get the desired outcome next.

Multiply decimals in a Shell Script

Use a text editor, we have used nano text editor and issue the following in terminal –


and, append the following lines of code –

read -p "Enter first number: " a
read -p "Enter second number: " b

multiply=`echo "$a * $b" | bc`
echo "The multiplication of numbers $a and $b is: " $multiply

save the file and exit.

The first line #!/bin/bash makes sure that BASH command-line interpreter is used. Thereafter, we use read built-in bash command to get the input from the user. The input is stored in two variables a and b. In addition to, we use bc command-line utility to multiply both the numbers. Finally, the result is printed through echo command.

Next, we need to execute the script. There are two methods to do that –

I. either pass script as an argument to the shell to execute the script –


II. or, change script permissions with –

chmod u+x

and, to execute the script


Lastly, once we execute the script, it will prompt us to input values. At the end, it would show us the decimal output if we have entered decimal input.

In conclusion, we have discussed how to multiply decimals in a Shell Script.

Similar Posts