Calculating the sum of two numbers in a shell script is easy, just like in other programming languages. The Bash shell has a command called expr that you can use to add numbers. Newer versions of Bash can do calculations directly without needing any extra commands.
In this tutorial, we will show you a few methods to add two numbers in a Bash script. You will learn how to use expr and the built-in features of the Bash shell to calculate the sum. This will help you understand how to do basic math operations in your shell scripts.
1. Adding Two Numbers
To add two numbers in a shell script, you can use the expr
command or the built-in Bash features. Below is an example of how to add two numbers using both methods. This will help you understand how to perform basic math in your shell scripts.
Use this syntax to add two numbers in a shell script:
- Using
expr
command with backquotes (` `):sum=`expr $num1 + $num2`
- Use
expr
command enclosed with brackets and start with dollar symbol:sum=$(expr $num1 + $num2)
- This is my preferred way to directly with the shell:
sum=$(($num1 + $num2))
In the next few examples, we will discuss calculating the sum of numbers directly with a shell. You can also choose expr command to give the syntax above.
2. Calculate Sum in Shell Script
Bash shell also evaluates the mathematical expressions directly. You just need to write the expressions enclosed in double brackets with a dollar like $((...))
.
Write an example shell script to initialize two numeric variables. Then perform an addition operation on both values and store results in the third variable.
#!/bin/bash
# Calculate the sum of two integers with pre initialize values
# in a shell script
# Initialize two variables with number
a=10
b=20
# Calculate sum
sum=$(( $a + $b ))
# Display the result
echo "Sum is: $sum"
Output:
Sum is: 30
3. Calculate Sum with Command Line Arguments
In this second example, the shell script reads two numbers as command line parameters and performs the 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
# Calculate sum
sum=$(( $1 + $2 ))
# Display the result
echo "Sum is: $sum"
Let’s execute this script is a shell
bash sum.sh 12 14
# Executing script Sum is: 26
4. Calculate Sum with Run Time Input
Here is another example of a shell script, which takes input from the user at run time. Then calculate the sum of given numbers and store to a variable and show the results.
#!/bin/bash
# Take input from user and calculate sum.
read -p "Enter first number: " num1
read -p "Enter second number: " num2
# Calculate sum
sum=$(( $num1 + $num2 ))
# Display the result
echo "Sum is: $sum"
Output:
Enter first number: 12 Enter second number: 15 Sum is: 27
Conclusion
In conclusion, adding two numbers in a shell script is simple and can be done using the expr
command or directly with the built-in features of the Bash shell. Understanding these methods is essential for beginners as it helps you perform basic math operations in your scripts. Whether you use expr
or the built-in Bash syntax, both methods are effective and easy to use. Practice these examples to become more comfortable with shell scripting and enhance your ability to automate tasks in the command line.
4 Comments
How to add two float value using shell script
Bro use this header file at the top of your code
#!/bin/bash
code :
echo -n “enter the num1 ==> ”
read num1
echo -n “enter the num2 ==> ”
read num2
sum =$(( $num1 + $num2 ))
echo “the sum of $num1 and $num2 is ==> $sum”
i have written this code but it shows me some errors so can you help me ?
error :
enter the num1 ==> 10
enter the num2 ==> 20
sum: ‘=30’: No such file or directory
the sum of 10 and 20 is ==>
Hi Dixit,
Remove space after “sum variable”
The full script should be