Shell Script to Add Two Numbers using ommand line arguments

In shell script to add two numbers: This tutorials will help you to step by step learn to add two numbers in the bash script. This simple script takes the input of two numbers from the user side as well as prints the total sum of All numbers.

Question: write a shell script to add two numbers. get inputs from user and display the sum.

Shell Script to Add Two Integers

This is an tutorials script initializes two variables with numeric values. After that executes shell script for sum means an addition operation on both Integers values and store data outputs in the onther variable.

#addition in shell script
# Addition of Calculate the sum of two integers with pre initialize values
# in a shell script bash addition
addition=$(( $f1 + $f2 ))
echo $addition

Command Line Arguments

In this another very useful example, shell script reads two Integers numbers as command line parameters as well as perform the sum or bash addition operation.

# Calculate the sum via command line arguments
# $1 and $2 refers to the first and second argument passed as command line arguments
addition=$(( $1 + $2 ))
echo "Addition is: $addition"  


$ ./ 33 15       # Executing script

Addition is: 48

Run Time Input

And Last is Run Time Input example with demo of a shell script program, which takes input from the user at run time. Then calculate the sum of given Integers numbers and store to a variable and display the outputs.

# Here simple Take input from user and calculate sum.
read -p "Please Enter Your first number: " firstno
read -p "Please Enter Your second number: " secondno
addition=$(( $firstno + $secondno ))
echo "Addition is: $addition"  


Please Enter Your first number: 33
Please Enter Your second number: 15
Addition is: 48

I hope you get an idea about shell script to add two numbers.
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.

Related FAQ

Here are some more FAQ related to this Article:

  1.   JavaScript Image Thumbnails Preview Gallery
  2.   Vue component render function using vuejs
  3.   Calculate post read time using PHP Example
  4.   PHP MySQL CRUD Create, Insert, Update and Delete operations
  5.   HTML Form Enctype Multipart Attribute with Examples
  6.   get product with Rakuten Marketing API using PHP
  7.   jquery Increment Animating Numbers Counting Up Example
  8.   Laravel 5/6/7 Import & Export data to Excel CSV using maatwebsite
  9.   Jquery File Upload Size validation Example
  10.   convert string to array python
Tags:, , , , ,