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 )
bash
man page section on "Command Subsitution". – larsks Aug 10 '15 at 18:2517
) or are you trying to get the string (5+12
)? – user1794469 Aug 10 '15 at 18:36x1=4; x2=3; x=$((x1+x2)); echo $x
– Cyrus Aug 10 '15 at 19:02$x1+$x2
to a variable, and you somehow decided piping is the right way to do that – Michael Mrozek Aug 10 '15 at 19:44cat -e 1\\t2\\t3 | cut -f1
. For assigning the output of a command to a variable, usevariable=$( command )
– FelixJN Aug 10 '15 at 20:08