how to pipe bc-calculation into shell variable

Seb picture Seb · Apr 15, 2011 · Viewed 25.7k times · Source

I have a calculation on a Linux shell, something like this

echo "scale 4;3*2.5" |bc

which gives me an result, now I like to pipe the result of this calculation into an Variable so that I could use it later in another command,

piping into files work, but not the piping into variables

echo "scale=4 ; 3*2.5" | bc > test.file

so in pseudo-code i'm looking to do something like this

set MYVAR=echo "scale=4 ; 3*2.5" | bc ; mycommand $MYVAR

Any ideas?

Answer

bmk picture bmk · Apr 15, 2011

You can do (in csh):

set MYVAR=`echo "scale 4;3*2.5" |bc`

or in bash:

MYVAR=$(echo "scale 4;3*2.5" |bc)