In shell script to add two numbers: This tutorials will help you to step by step learn to add two numbers in the bash script. This simple script takes the input of two numbers from the user side as well as prints the total sum of All numbers.
Question: write a shell script to add two numbers. get inputs from user and display the sum.
Shell Script to Add Two Integers
This is an tutorials script initializes two variables with numeric values. After that executes shell script for sum means an addition operation on both Integers values and store data outputs in the onther variable.
#!/bin/bash #addition in shell script # Addition of Calculate the sum of two integers with pre initialize values # in a shell script bash addition f1=40 f2=60 addition=$(( $f1 + $f2 )) echo $addition
Command Line Arguments
In this another very useful example, shell script reads two Integers numbers as command line parameters as well as perform the sum or bash addition operation.
#!/bin/bash # Calculate the sum via command line arguments # $1 and $2 refers to the first and second argument passed as command line arguments addition=$(( $1 + $2 )) echo "Addition is: $addition"
$ ./sum.sh 33 15 # Executing script Addition is: 48
Run Time Input
And Last is Run Time Input example with demo of a shell script program, which takes input from the user at run time. Then calculate the sum of given Integers numbers and store to a variable and display the outputs.
#!/bin/bash # Here simple Take input from user and calculate sum. read -p "Please Enter Your first number: " firstno read -p "Please Enter Your second number: " secondno addition=$(( $firstno + $secondno )) echo "Addition is: $addition"
Please Enter Your first number: 33 Please Enter Your second number: 15 Addition is: 48
I hope you get an idea about shell script to add two numbers.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.
Here are some more FAQ related to this Article: