0

I have small problem with use bc command in unix. I have two varaibles: variable1, variable2. The arithmetic expression looks like:

res=$$((($variable1*10)/$variable2)

I would like to round the result from two divided numbers. I think, the best solution will be using bc -l command + scale=X, but doesn't work.

res=$$(((echo "scale=2; $variable1*10)/$variable2" | bc -l)

I would like to get more exact result. Now, f.e., I have:

res = 10

But should be

res = 9.23
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
profiler
  • 567
  • 2
  • 15
  • 41
  • 2
    "doesn't work" is *not* a very good problem report. You should be specifying what you want it to do and what it actually does (in some detail). – paxdiablo Aug 02 '15 at 08:26
  • True, was changed some information. I would like to get more precise result. – profiler Aug 02 '15 at 08:40

1 Answers1

2

What you currently have won't work simply because the parentheses in the bc expression are unbalanced. In addition, you appear to have way more $, ( and ) characters in there than you need.

Without those flaws, it works fine:

pax> num=923
pax> den=1000
pax> res=$(echo "scale=2; $num * 10 / $den" | bc -l)
pax> echo $res
9.23
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953