Skip to content

Linux bc command

The Linux bc command is a versatile tool for performing mathematical calculations in the terminal. Whether you need to do basic arithmetic operations like addition, subtraction, multiplication, and division or more complex calculations involving advanced mathematical functions, bc has got you covered. This guide will walk you through the basics of using bc, including syntax and command options, so you can harness the full power of this handy utility. Enhance your productivity and efficiency by mastering the bc command in Linux.

bc Syntax:

Terminal window
bc [options] [file]

Options:

OptionDescription
-h, —helpDisplay help message and exit
-iStart bc in interactive mode
-qTurn off all warnings
-sEnable standard math library functions

Parameters:

ParameterDescription
fileSpecify a file to read and execute commands from

bc Usage:

Simple Arithmetic Calculation

Terminal window
echo "5 + 3" | bc

Performs a simple addition calculation with the bc command.

Calculate Square Root

Terminal window
echo "sqrt(25)" | bc -l

Calculates the square root of 25 using the -l option to load the math library in bc.

Perform Trigonometric Functions

Terminal window
echo "s(45)" | bc -l

Uses the bc command with the -l option to perform trigonometric functions, such as calculating the sine of 45 degrees.

Calculate Exponential Function

Terminal window
echo "e(2)" | bc -l

Calculates the exponential function of 2 using the math library in bc.

How do I use bc in Linux?

To use the bc command in Linux, execute the following command:

Terminal window
bc -l <<< "10 * 2"

What is the purpose of the -l option in bc?

The -l option in the bc command is used to load the math library, enabling access to additional mathematical functions like sine, cosine, and square root.

Terminal window
bc -l <<< "s(0.5)"

How can I calculate with a specific scale or precision in bc?

You can set the scale or precision in bc using the “scale” variable. For example, to perform calculations with a precision of 5 decimal places:

Terminal window
echo "scale=5; 7 / 3" | bc

Can bc be used for floating-point arithmetic in Linux?

Yes, the bc command in Linux supports floating-point arithmetic, making it suitable for precise mathematical calculations involving decimal numbers.

Terminal window
bc <<< "5.2 * 3.7"

How can I perform exponentiation in bc?

To calculate exponentiation in bc, you can use the power operator (^). For example, to compute 2 to the power of 3:

Terminal window
bc <<< "2^3"

Is there a way to use bc within a script for automation?

Yes, you can incorporate bc commands within bash scripts to automate mathematical calculations and process outputs as needed.

#!/bin/bash
result=$(bc <<< "4 * 6")
echo "The result is: $result"

How can I use bc for factorial calculations in Linux?

To calculate the factorial of a number using bc in Linux, you can utilize the factorial (factorial) function. For example, to determine the factorial of 5:

Terminal window
echo "factorial(5)" | bc -l

Applications of the bc Command

  • Perform arithmetic operations
  • Calculate mathematical expressions
  • Support for mathematical functions
  • Precision control for calculations
  • Interactive mode for on-the-fly calculations