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
Software requirements and conventions used
Category | Requirements, Conventions or Software Version Used |
---|---|
System | Linux Distribution-independent |
Software | Bash command line, Linux based system |
Other | Any utility which is not included in the Bash shell by default can be installed using sudo apt-get install utility-name (or yum install for RedHat based systems) |
Conventions | # – requires linux-commands to be executed with root privileges either directly as a root user or by use of sudo command$ – requires linux-commands to be executed as a regular non-privileged user |
Installing bc
To install bc on Ubuntu, Mint or any other Debian/APT based operating system, type:
$ sudo apt install bc
To install bc on Fedora, RedHat or any other RedHat/YUM based operating system, type:
$ sudo yum install bc
Default Bash Calculations
We can do a simple division problem directly in Bash:
$ echo $[ 13 / 4 ] 3
This tries and divides 13 by 4 using the standard Bash Idiom $[ calculation ]
. Whilst this is quite versatile:
$ echo "1+1? The answer is: $[ 1 + 1 ]" 1+1? The answer is: 2
It regrettably is unable to output decimals. Workarounds to this problem are complex and ugly as described earlier.
bc to the Rescue
A more elegant solution is to use bc
for calculations.
Whilst bc can also be used for the same calculations as already possible in Bash:
$ echo '13 / 4' | bc 3
It is also able to produce decimal based outcomes using the -l
(-l
defines the standard math library) option to bc
:
$ echo '13 / 4' | bc -l 3.25000000000000000000
Storing outcomes as variables
We can also store out outcome as a variable by using a subshell:
$ OUTCOME=$(echo '13/4' | bc -l) $ echo ${OUTCOME} 3.25000000000000000000
Here we used the same calculation as in the last example, but we used a subshell $()
to do the calculation. This allows us to store the outcome in a variable easily. We can now use this variable further in other calculations, or simply output the result as shown above. To use it in another calculation, you can:
$ echo "${OUTCOME} * 4" | bc -l 13.00000000000000000000
Please note how we used double quotes this time to define our arithmetic. The reason for this is that we want the ${OUTCOME}
variable to be substituted for it’s actual value. If we had used single quotes ('
) instead, the operation would have failed as the variable name would have been taken to be a literal input. In other words, no substitution to it’s value would have happened, as we can see from the following output:
$ echo '${OUTCOME} * 4' | bc -l (standard_in) 1: illegal character: $ (standard_in) 1: syntax error
Ready to learn more Bash? Explore our Useful Bash Command Line Tips and Tricks Series!
Conclusion
In this article, we explored how to make decimal based calculations with bc
at the command line. The same commands work fine when used from within a shell script. We also looked at the shortcomings of using standard Bash calculation idioms, as well as using variables in combination with bc
using subshells. Finally we discovered what happens if one incorrectly quotes variables in Bash.