How to calculate bc calculation in shell variable

I have a linux shell calculation, something like this

echo "scale 4;3*2.5" |bc

which gives me the result, now I like to pass the result of this calculation to a variable so that I can use it later in another command,

Working with files in files, but not connecting to variables

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

so in pseudo code i'm looking for something like this

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

Any ideas?

+5
source share
3 answers

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)
+6
source
MYVAR=`echo "scale=4 ; 3*2.5" | bc`

Note that bash does not like non-integer values ​​- you will not be able to perform calculations from 7.5 in bash.

+2
source
 MYVAR=$(echo "scale 4;3*2.5" | bc)
0

All Articles