Floating Point Math in Linux Bash
In this article
Bash itself cannot do floating-point arithmetic — it only handles integers. To get decimal results in a Linux shell script, you must use an external tool like bc, awk, or python3. This article explains why Bash has this limitation, how to work around it, and which tool fits different situations.
Why Bash Only Does Integers
Bash's built-in arithmetic is handled by the shell's (( )) and $(( )) operators, which are designed for integer math. The POSIX specification for shell arithmetic requires only signed long integers — no fractions, no decimals. This keeps the shell lightweight and fast, since it doesn't need to link in a floating-point library. When you write echo $((5 / 2)), Bash performs integer division and truncates the remainder, printing 2.
This behavior is intentional. The shell's job is to orchestrate commands, not to be a scientific calculator. For anything requiring decimal precision, the Unix philosophy says to delegate to a dedicated tool. That is why commands like bc and awk exist.
Using bc for Arbitrary Precision
The bc utility is a command-line calculator that supports arbitrary precision. It reads expressions from standard input and prints the result. To get a decimal division, you must set the scale variable to the number of digits you want after the decimal point.
echo "scale=2; 5 / 2" | bcThis outputs 2.50. Without scale, bc defaults to integer output. You can also use bc -l to load the math library, which sets scale to 20 and adds functions like s() (sine), c() (cosine), and l() (natural log).
For arithmetic with variables, you can pass them into the expression:
a=5; b=2; echo "scale=3; $a / $b" | bcThis prints 2.500. Note that bc does not have a built-in square root unless you use -l, which provides sqrt().
Using awk for Fast Floating Point
If you need a quick floating-point calculation without piping to an external calculator, awk is a strong choice. Awk uses the C double type for all numbers, so it handles decimals natively. A typical one-liner looks like:
awk 'BEGIN { print 5 / 2 }'This prints 2.5. To use shell variables, pass them with -v:
a=5; b=2; awk -v x="$a" -v y="$b" 'BEGIN { print x / y }'Awk's output format defaults to six significant digits, which is fine for most scripts. You can control formatting with printf:
awk 'BEGIN { printf "%.2f\n", 5 / 2 }'This prints 2.50. Awk also handles arrays, loops, and conditionals, making it useful for more complex data processing, not just single calculations.
Other Tools: Python, Perl, and printf
For modern systems, python3 is a reliable option if it is installed. It provides full IEEE 754 floating-point arithmetic and easy formatting:
python3 -c 'print(5 / 2)'This outputs 2.5. For rounding, use round() or format strings. Python is heavier than bc or awk, so it is best for scripts that already require Python.
Perl is another alternative, though less common in new scripts:
perl -e 'print 5 / 2'Finally, Bash's printf can round integers to a fixed number of decimal places, but it does not perform division. You still need an integer numerator and denominator. For example:
printf "%.2f\n" $((5 * 100 / 2))This prints 2.00 because the integer division happens first. To get 2.50, you must scale the numerator before dividing:
printf "%.2f\n" $((500 / 2))This prints 2.50. This trick works for simple cases but fails for large numbers or when you need many digits.
Common Pitfalls and Precision Issues
One frequent mistake is forgetting that bc requires scale to be set. Without it, echo "5 / 2" | bc returns 2, not 2.5. Another is using expr, which also only does integer arithmetic.
Precision matters: awk uses double-precision floating point, which has about 15-17 significant decimal digits. For most shell scripts, that is plenty. If you need exact decimal arithmetic — for financial calculations, for example — bc with a high scale is safer because it uses arbitrary-precision decimal numbers, avoiding binary rounding errors.
For example, awk 'BEGIN { print 0.1 + 0.2 }' might print 0.3, but due to binary representation it could print 0.30000000000000004 on some systems. bc with scale=2 would print .3 (or 0.3 with a leading zero if you set scale to 1).
Another pitfall is integer overflow in Bash's $(( )). On 64-bit systems, integers are 64-bit, so values up to 9,223,372,036,854,775,807 are safe, but exceeding that wraps around. Floating-point tools do not have this limit for practical purposes.
Choosing the Right Tool for Your Script
- Quick one-off division: Use
awk— it is fast, built-in, and handles decimals natively. - Arbitrary precision or financial math: Use
bcwith a highscaleto avoid binary rounding errors. - Complex math functions (sqrt, log, trig): Use
bc -lorpython3. - Already using Python in the script: Use
python3 -cto avoid spawning another process. - Very large or very small numbers: Use
bcorpython3;awkmay lose precision.
Performance is rarely a concern for a few calculations, but if you are processing millions of lines, awk is generally faster than spawning bc for each line. For heavy numerical work, consider a dedicated language like Python or Perl.
Frequently Asked Questions
Can I make Bash do floating point natively?
No. Bash's arithmetic is integer-only by design. There is no built-in option to enable floating point. You must use an external command like bc or awk.
Why does echo $((5/2)) output 2?
Because Bash performs integer division and truncates the fractional part. The result is the quotient without the remainder. To get a decimal, use echo "scale=2; 5/2" | bc or awk 'BEGIN { print 5/2 }'.
What is the difference between bc and awk for floating point?
bc uses arbitrary-precision decimal arithmetic, so you control the number of digits with scale. awk uses double-precision binary floating point, which is faster but can have small rounding errors. For most shell scripts, either works; for exact decimal math, use bc.
How do I round a floating-point result in Bash?
You can use printf with a format specifier. For example, printf "%.2f\n" $(echo "scale=4; 5/3" | bc) rounds to two decimal places. Alternatively, use awk 'BEGIN { printf "%.2f\n", 5/3 }'.
Is there a way to use floating point in a Bash if condition?
Bash's [[ ]] and (( )) only handle integers. To compare floats, use awk or bc. For example: if awk 'BEGIN { exit !(5/2 > 2.4) }'; then echo "greater"; fi.
Does expr support floating point?
No. expr only handles integers. It will error or truncate on decimal input. Use bc or awk instead.