6

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?

sjas
  • 18,644
  • 14
  • 87
  • 92
Seb
  • 173
  • 2
  • 2
  • 7

3 Answers3

9

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)
bmk
  • 13,849
  • 5
  • 37
  • 46
2
MYVAR=`echo "scale=4 ; 3*2.5" | bc`

Note that bash doesn't like non-integer values - you won't be able to do calculations with 7.5 in bash.

Erik
  • 88,732
  • 13
  • 198
  • 189
0
 MYVAR=$(echo "scale 4;3*2.5" | bc)
Sven
  • 22,475
  • 4
  • 52
  • 71