Q:

UNIX evaluate expression from a variable

I have a variable which has a math expression.  I want to evaluate it in UNIX shell and store the result in another variable.  how do i do that ? 
i tried the following but it doesn't works
var1="3+1"
var2=`expr "$var1"`
echo $var2

the var2 value should be calculated as 4. 

A:

expr requires spaces between operands and operators.  Also, you need backquotes to capture the output of a command.  The following would work:
var1="3 + 1"
var2=`expr $var1`
echo $var2

If you want to evaluate arbitrary expressions (beyond the limited syntax supported by expr) you could use bc:
var1="3+1"
var2=`echo $var1 | bc`
echo $var2

A:

You can do it this way
var2=$(($var1))

