#!/bin/bash
x=10
echo x=$x
z=20
echo z=$z
y= expr $x + $z
echo y=$y
I want output like :
x=10
z=20
y=30
but it gives error like:
x=10
z=20
30
y=
Do NOT use the outdated construct expr, use the arithmetic operator $(()) for POSIX compliant arithmetic in bash
y=$((x + z))
echo "y=$y"
$((...)) is not just standard, but it performs the arithmetic in the current process, rather than starting a new process to run expr.y=$((x + z)) with ((y = x + z)) or simply y=x+z if you declare x, y and z as integers prior with declare -i x y z
y=$(expr $x + $z)instead ofy= expr $x + $zy=`expr $x + $z`also works, looking for "Command Substitution in bash", is better Inian's solution way