Seb
Seb

Reputation: 193

how to pipe bc-calculation into shell variable

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?

Upvotes: 8

Views: 30928

Answers (3)

bmk
bmk

Reputation: 14147

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)

Upvotes: 10

Erik
Erik

Reputation: 91270

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.

Upvotes: 2

anon
anon

Reputation:

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

Upvotes: 0

Related Questions