How to add two variables in shell script

1. initialize two variables

2. Add two variables directly using $(...) or by using external program expr

3. Echo the final result.




Add two variables without using expr in shell script

#shell program to add two variables

var1=10
var2=20

sum=$(($var1 + $var2))

echo $sum

Output

30




Add two variables using expr in shell script

#shell program to add two variables

var1=10
var2=20

sum=`expr $var1 + $var2`

echo $sum

Output

30


Useful Resources

To learn more shell script examples, you can visit the link