Shell script to add two numbers. get input from user and display the sum

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.

Free Live Chat for Any Issue

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.

#!/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.

#!/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.

I'm learning Shell scripting for a diploma in IT I'm currently doing. I'm trying to write a small script that adds two numbers as shown as in one of the tutorials we were given.

echo "Enter two numbers" read num1 num2 sum = 'expr $num1 + $num2' echo "The sum is = $sum"

However when I give it the execution permission and run the script, it gives me this error.

sum: =. No such file or directory. sum: expr $num1 + $num2: No such file or directory

Shell script to add two numbers. get input from user and display the sum

I tried running this on both Ubuntu and Fedora but same error occurs. Can anyone please tell me what I'm missing here?

1

Assuming that the inputs are given as command line arguments and if two numbers are not given show a error message as "command line arguments are missing".

Sample output:

addition of 1 and 2 is 3.

4

Using function keyword, we can declare function in shell script.

In this tutorial, we will learn how to add two numbers using function in shell script.



function fun_name () { #Implement the function here }

To call the function, just add the function name in the script.

> fun_name



To call the function with arguments, add the argument values after the function name.

Like,

> fun_name arg1 arg2

In function $1 will refer the value of agr1, and $2 will refer the value of arg2 and so on.


1. Initialize two variables.

2. Declare and implement the addition function.

3. Call the add function with two arguments.



Add two variables using function in shell script

#$1 will have the value 10 which is the value of a #$2 will have the value 20 which is the value of b function add() { sum=$(($1 + $2)) echo "Sum = $sum" } a=10 b=20 #call the add function and pass the values add $a $b

Output


In shell script we can pass the arguments from command line.

In this tutorial, we will learn how to add two numbers using command line arguments.



To pass arguments from the command line, add the argument values after the file name while executing the script.

Like,

sh file_name.sh arg1 arg2

In script $1 will refer the value of agr1, and $2 will refer the value of arg2 and so on.



Algorithm

1. Pass the arguments while executing the script.

2. Get first arguments using $1 and second argument using $2.

3. Add $1 and $2 and display the result.



Add two variables using command line argument in shell script

#shell script to add two numbers #using command line arguments #$1 refers to the first argument #$2 refers the second argument and so on sum=$(($1 + $2)) echo "Sum = $sum"

Run the script

sh add.sh 10 20


sh add.sh 100 20