5

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: $
Hrvoje Špoljar
  • 5,245
  • 26
  • 42
watchmansky
  • 769
  • 4
  • 10
  • 17

2 Answers2

6

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
Hrvoje Špoljar
  • 5,245
  • 26
  • 42
1

Try with:

A=12;echo 'scale=2;'"$A / 5"|bc -l
Pol Hallen
  • 1,095
  • 2
  • 13
  • 24