How to Make Decimal Calculations In Bash Using bc - JohnHau/mis GitHub Wiki

Decimal calculations are sometimes required in Bash. The standard calculation Bash programming idiom ($[]) is unable to provide a decimal output. Whilst we can trick it into calculating (but not generating) a decimal output by multiplying the numbers by for example a factor of 1000 and then doing an text based splitting, this is a ugly workaround and creates complex code. There is however a utility in Bash which can natively do decimal based calculations without any tricks or workarounds!

In this tutorial you will learn:

How to use bc to perform decimal calculations How to make decimal based calculations at the Bash command line or from in your scripts How to use variables to store the results produced by bc How to use variables in further calculations How to avoid Bash variable quoting errors

image

image

image