Home »
Linux »
Linux shell script programs
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 statement
Here, we will create a shell script program to add two numbers that will be passed on the command line with the program name.
Add two numbers using command line arguments
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.
Linux shell script program to add two numbers using command line arguments
#!/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 »