How to Use the bc Command as a Command-Line Calculator in Linux

When working in a headless Linux server environment, you do not have access to a graphical desktop calculator app. If you need to perform quick mathematical operations—like calculating server uptime ratios, bandwidth conversions, or basic arithmetic—you do not need to write a full Python script. You can simply use the built-in bc (Basic Calculator) command.

How to Launch the Interactive Calculator

The bc command can operate as a full, interactive terminal session.

  1. Open your Linux terminal.
  2. Type bc and press Enter.
  3. A short copyright header will appear, and your cursor will drop to a blank line, waiting for input.

You can now type any mathematical expression directly into the terminal, such as 150 * 4 or 1024 / 8. When you press Enter, the system will instantly print the calculated result directly beneath your equation. To exit the interactive session and return to your standard bash prompt, simply type quit or press Ctrl + D.

How to Use bc for One-Off Calculations

If you are writing a bash script or simply want an answer without entering the interactive mode, you can pipe a mathematical equation directly into the bc command using echo.

For example, if you want to calculate 25% of 500, type the following single line:

echo "500 * 0.25" | bc

The terminal will instantly output 125.00 and return your normal command prompt.

Enabling Floating Point Math

By default, bc operates as an integer calculator. This means if you type 10 / 3, it will confidently output 3, completely ignoring the decimals.

To force the calculator to respect floating-point decimals, you must launch it with the -l (math library) flag.

bc -l

Or, in a one-liner:

echo "10 / 3" | bc -l

This will output the highly precise 3.33333333333333333333, ensuring your server calculations are completely accurate.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.