Expr in Bash for Multiplication and Division of Numbers
The expr command in Bash reads and evaluates the expression parameters and writes the result to standard output. The syntax of the expr command is:
Multiplication of Numbers in Bash Using the Expr Command
Although “*” symbolizes multiplication, “*” in Bash represents all files in the current directory. If you use “*” directly with the expr for the multiplication of two numbers in the shell, it gives you an error. So, to multiply the numbers in Bash, use “\*’” instead of “*”.
The following example explains how you can multiply the numbers in Bash using the expr command:
#Multiplication of integers using the expr command
A=25
B=5
echo "Multiplication of A and B is (A x B) = AB"
echo "AB= `expr $A \* $B`"
Output:
Division of Numbers in Bash Using the Expr Command
Let’s divide the numbers in Bash using the “/” symbol. The following example will give you a better clarification:
#Division of integers using the expr command.
A=25
B=5
echo "A / B = `expr $A / $B`"
Output:
Conclusion
This is how you can multiply and divide the numbers using the expr command in Bash. Creating arithmetic calculations in Bash is simple, and we recommend that you learn these arithmetic operations as beginners. This practice can help you gain a hands-on experience with the Bash scripts. For more information on Bash and shell scripting, please visit the Linuxhint website. We uploaded hundreds of tutorials that are related to Bash and other programming languages.