🚀 90+ coding courses. 200+ competitive exams. One intelligent platform — Leyaa.ai →

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


🚀 Your coding + exam prep journey starts here

Learn Python, C, Java, SQL & 90+ technologies. Practice for IBPS, SSC, Railway & 200+ exams. All powered by AI. All free.

Get Started on Leyaa.ai →

Topics You Might Like