In this tutorial, we will discuss a few methods to calculate the sum of the two numbers in a bash script.

## Bash – Adding Two Numbers

The expr is the command-line utility used for evaluating mathematical expressions. Bash shell also supports evaluating the mathematical expressions directly. Use the following syntax to calculate the sum of two integers in a shell script:

Using expr command with quotessum=`expr $num1 + $num2`

Use expr command inclosed 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.

## 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.

Output:

## 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.

Let’s execute this script is a shell

## 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.

Output:

## Conclusion

In this tutorial, you have learned few methods to add two numbers in a shell script.