in , ,

LinuxLinux

Add Two Numbers in Shell Script Bash Linux!

How to add two numbers in shell script in Linux

Add Two Numbers Using Shell Script

A

dd Two Numbers Using Shell Script: Bash or Shell Script is the command line language & Unix Shell. The Linux, Unix & Mac OS adopt this technology and made their default shell as Bash Shell Scripting. In this tutorial, you will learn about adding two number in shell script with basic formulae and working example.

Jump To:

Formulae To Add Two numbers in Shell:

The below script will takes the input from the user and add the input values and print the values in the new variable. This script has three variables totally, Two variables are used to receive the input from the user and the third variable is used to print the added value of the two variable.

Shell Script 1:

#!/bin/bash
a=5
b=10

add= $(( $a + $b ))

echo $add

The above script is used to add the two predefined values and print the added value in the third variable.

Shell Script 2:

#!/bin/bash
add= $(( $a + $b ))

echo "Sum of two number is: $add"

In the above script, The $a & $b represents the first & second argument passed as the command line arguments. The output value will print in the third variable $add.

The sample output of the above script is,

Example
./add.sh 24 32  #The Values of the two variable are passed as command line arguements.

Sum of two number is: 56

Example of Adding Two numbers in Shell:

This is the example script to receive the values for the variables from the user in runtime compilation. Which receives the two variables from the user and print the added value in the third variable.

#!/bin/bash
read -p "Enter the first number: " a
read -p "Enter the second number: " b

 #Received the values from the user
add=$(( $a + $b ))           #add the values
 
echo "Sum of two number is: $add"

The output of the above script is,

Output
Enter the first number: 5
Enter the second number: 10
Sum of two number is: 15

From the above output, The Script received the values for the two variables from the user and add the two values and print the added value in the third value. In this way, you can use the shell script to add the two variables or number in the shell script.

Check this too  How To Check Two Strings Are Equal Using Bash Shell Script

Verdict:

That’s it. You can use the above scripts to add the two values or numbers in a shell script in Linux. If you have any queries related to this topic and then feel free to comment us below.

Article Review
  • Formulae Explanation
  • Example Provided
4.8

Summary

For more articles, Kindly Visit our HomePageHomePage

Leave a Reply

Your email address will not be published. Required fields are marked *

Written by Julian

Hello Folks! I'm Julian, Promoted to the Content writer for the category of Commands & Web Apps in Tec Robust. Enjoy my content & Learn more Linux Commands!

Contact me:
tecrobust [@] gmail.com
Sub: Message to Julian

ls Command

ls Command: How To List Files in Linux Using ls Command

Magnus Ubuntu

Magnus: How To Install Magnus for Ubuntu 18.04 LTS and 19.04