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.

Read Also:  JavaScript Image Thumbnails Preview Gallery

#!/bin/bash
#addition in shell script
# Addition of Calculate the sum of two integers with pre initialize values
# in a shell script bash addition
 
f1=40
f2=60
 
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.

#!/bin/bash
# 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"  

Output:

$ ./sum.sh 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.

Read Also:  Allow Only Numbers in TextBox Windows Forms using regular expression

#!/bin/bash
# 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"  

Output:

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 infinityknow.com 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.

Read Also:  Laravel 5/6/7 Import & Export data to Excel CSV using maatwebsite

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  PHP cURL Set custom header Authorization
  2. Read Also:  how to check a plugin is activated or not in wordpress
  3. Read Also:  Creating Custom Database Tables plugin or theme using WordPress
  4. Read Also:  Vuejs File Type extension Validation
  5. Read Also:  create more than one new company for Frontaccounting
  6. Read Also:  get product with Rakuten Marketing API using PHP
  7. Read Also:  PHP Compare Two Arrays Values
  8. Read Also:  Export Json Array to CSV using php Example
  9. Read Also:  How to Create a Currency Converter using Pipe with Angularjs?
  10. Read Also:  Clear Caches Laravel 5.7 using command line
CLOSEX