开发者

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 n开发者_StackOverflowot 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?


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)


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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜