How can I assign value of $x1+$x2 to bc by piping? here in my code $bc holds no value.
echo -n 'value x1 : '
read x1
echo -n 'value x2 : '
read x2
echo $x1+$x2 | bc
echo $bc
How can I assign value of $x1+$x2 to bc by piping? here in my code $bc holds no value.
echo -n 'value x1 : '
read x1
echo -n 'value x2 : '
read x2
echo $x1+$x2 | bc
echo $bc
is easy and there are many ways to do, for example
v=$(echo $x1+$x2 | bc)
v=`echo $x1+$x2 | bc`
Note that bc is integer arithmetics only and that you need bc -l for a proper math library. Note that you can skip the echoing with the 'here' redirection <<< for strings:
v=$( bc <<< $x1+$x2 )