ADVERTISEMENT
ADVERTISEMENT

Linux shell script program to add two numbers using command line arguments

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

Problem Solution:

Here, we will create a shell script program to add two numbers that will be passed on the command line with the program name.

Program/Source Code:

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

#!/bin/bash
# Program name: "command_line_add.sh"
# shell script program to add two numbers using command line arguments.
sum=`expr $1 + $2`
echo "Sum is: $sum"

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

Output:

$ sh command_line_add.sh 15 20
Sum is: 35 

Explanation:

In the above program, we passed two integer numbers as command-line arguments. Here, we used both command-line integer numbers using $1 and $2 variables and then perform an addition operation and printed the result on the console screen.

Linux shell script programs »

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.