ADD TWO NUMBERS IN SHELL SCRIPT
In this shell script article we are going to learn to:
-
add two numbers in shell script
Lets see the steps to find the sum of two numbers in unix shell script.
STEPS TO ADD NUMBERS IN SHELL:
Follow the below steps to add two numbers in unix:
-
Use ‘clear’ command to clear the screen.
-
Use ‘echo’ command to print output.
-
‘-n’ is used to keep the cursor in the same line.
-
Take two inputs and store it in two different variables.
-
You can use ‘echo’ or ‘expr’ to calculate arithmetic operation. But here I have used just dollar sign to perform operation.
-
At last just, print the result
CODE:
Given below is code to add two numbers in shell script
clear
echo -n "Enter 1st number: "
read first_number
echo -n "Enter 2nd number: "
read second_number
sum=$(($first_number + $second_number))
echo "Sum of $first_number and $second_number: "$sum
WRITTEN IN CYGWIN:
OUTPUT:
use ':wq' command to save and quit the files.
use 'bash' command to execute code.
Given below is video tutorial to add two numbers in shell script using command line arguments:
Would you like to see your article here on tutorialsinhand.
Join
Write4Us program by tutorialsinhand.com
About the Author
Page Views :
Published Date :
Feb 07,2021