3

Easily, I'm writing a script that needs some values with 2 digits after floating point. Trying to use bc I don't understand how use "scale", i.e.

A=12 ; bc <<< $(($A/5))

it's correct, but adding "scale" leads to an error:

A=12 ; bc <<< 'scale=2;$(($A/5))'

(standard_in) 1: illegal character: $
(standard_in) 1: illegal character: $

closed as off-topic by Iain, kasperd, masegaloeh, mdpc, Andrew Schulman Nov 16 '14 at 3:08

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must be relevant to professional system administration. Server Fault is dedicated to professional system and network administrators. End user and enthusiast questions are off-topic (contact your system administrator or hire a professional to help you out). Please see the Help Center for more information." – mdpc, Andrew Schulman
If this question can be reworded to fit the rules in the help center, please edit the question.

5

Replace single quotes with double; because with single quotes $A in your equation is not expanded, rather considered as literally $A not 12

A=12 ; bc <<< "scale=2;$(($A/5))"
2

Also, inside $(()) to variable does not need to be specified as $A, just A will do, e.g.

A=12 ; bc <<< "scale=2;$((A/5))"
2

Next, when doing $(()) you invoke subshell, which is not what you want to do because bc does not do anything then. Try this

A=12 ; bc <<< "scale=2; $A/5"
2.40
  • Hello, I modified my question. I need the result with floating point. Thanks! – watchmansky Nov 15 '14 at 23:07
1

Try with:

A=12;echo 'scale=2;'"$A / 5"|bc -l

Not the answer you're looking for? Browse other questions tagged or ask your own question.