Shell script for multiplication of two numbers

1. initialize two variables

2. multiply two numbers directly using $(...) or by using external program expr

3. Echo the final result.




Multiplication of two numbers without using expr

#shell program to multiply two numbers 

num1=10
num2=20

ans=$((num1 * num2))

echo $ans

Output

200




Multiplication of two numbers using expr in shell script

In shell, * represents all files in the current directory.

So, in order to use * as a multiplication operator, we should escape it like \*.

If we directly use * in expr, we will get error message.


#shell program to multiply two numbers using expr

num1=10
num2=20

ans=`expr $num1 \* $num2`

echo $ans

Output

200


Topics You Might Like