Linux shell script program to add two numbers

Here, we are going to learn how to add two numbers in Linux shell script?
Submitted by Nidhi, on February 14, 2021

Problem statement

Here, we will create a shell script program to add two integer numbers and then print the sum of numbers on the console screen.

Add two numbers using Linux Shell Script

The source code to create a Linux shell script program to add two numbers is given below. The given program is compiled and executed successfully on Ubuntu 20.04.

Linux shell script program to add two numbers

#!/bin/bash

# Program name: "add_two_num.sh"
# Shell script program to add two numbers.

num1=10
num2=20

num3=`expr $num1 + $num2`

echo "Sum is: $num3"

Now, we will save the shell script program with the "add_two_num.sh" name.

Output

$ sh add_two_num.sh 
Sum is: 30

Explanation

In the above program, we created two variables num1, num2 that are initialized with 10 and 20 respectively. Here, we used the expr command to add two values of variables and then print the sum of two numbers on the console screen.

Linux shell script programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.