bc: using “scale” with bash variable [closed] – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.
But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about linux, bash, scripting, shell, .
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: $
Replace single quotes with double; because with single quotes
$A in your equation is not expanded, rather considered as literally
A=12 ; bc <<< "scale=2;$(($A/5))" 2
Also, inside $(()) to variable does not need to be specified as
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
A=12;echo 'scale=2;'"$A / 5"|bc -l