how to add two decimal numbers in shell script?

Today, We want to share with you shell script to add two numbers.In this post we will show you write a shell script to add two variables using function., hear for create a shell script to add two numbers and also display odd number we will give you demo and example for implement.In this post, we will learn about Shell Script To Add Two Numbers Using Ommand Line Arguments with an example.

Shell Script to Add Two Integers

This is demo script initializes two variables with numeric values. And Then perform an addition operation on both values as well as store results in the third variable.

This demo will help you to understand to add two numbers in the bash script. This script takes the input of two numbers from the user as well as displays the sum of both numbers.

Also Read This πŸ‘‰   JavaScript GROUP BY Function with Example

Example 1:

# Calculate the sum of two integers with pre initialize values
# in a shell script


sum=$(( $a + $b ))

echo $sum

Command Line Arguments

In this second demo, shell script reads two (2) numbers as command line parameters as well as perform the addition operation.
Example 2:

# Calculate the sum via command line arguments
# $1 as well as $2 refers to the first as well as second argument passed as command line arguments

sum=$(( $1 + $2 ))

echo "Sum is: $sum"   

Free Live Chat for Any Issue


$ ./ 20 30       # running this script

Sum is: 50

Run Time Input

Example 3:
Here is another demo of a shell script, which takes input from the user at run time. And Then total the sum of given numbers as well as store to a variable as well as displays the outputs.

# Take input from user as well as total sum.

read -p "Enter first number: " num1
read -p "Enter second number: " num2

sum=$(( $num1 + $num2 ))

echo "Sum is: $sum"       


Enter first number: 41
Enter second number: 45
Sum is: 86

I hope you get an idea about sum of numbers in shell script.
I would like to have feedback on my blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.