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
2
  • 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). Commented Aug 2, 2015 at 8:26
  • True, was changed some information. I would like to get more precise result. Commented Aug 2, 2015 at 8:40

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Indeed, my problem was with to have to much signs $,(.). It works perfectly now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.