Articles

ADD TWO NUMBERS IN SHELL SCRIPT

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:

  1. Use ‘clear’ command to clear the screen.
  2. Use ‘echo’ command to print output.
  3. ‘-n’ is used to keep the cursor in the same line.
  4. Take two inputs and store it in two different variables.
  5. You can use ‘echo’ or ‘expr’ to calculate arithmetic operation. But here I have used just dollar sign to perform operation.
  6. At last just, print the result
add two numbers in shell script

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:

add two numbers in shell script

OUTPUT:

use ':wq' command to save and quit the files.

use 'bash' command to execute code.

add two numbers in shell script

Given below is video tutorial to add two numbers in shell script using command line arguments:

 


Shell Script Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Suraj Maity
Page Views :    Published Date : Feb 07,2021  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!