bc

calculatorlinux
The bc command is one of the most frequently used commands in Linux/Unix-like operating systems. bc The bc (basic calculator) is an arbitrary precision calculator language with syntax similar to C. It allows for interactive calculations as well as programming with variables, functions, and control statements.

Quick Reference

Command Name:

bc

Category:

calculator

Platform:

linux

Basic Usage:

bc [options] [arguments]

Common Use Cases

  • 1

    Mathematical calculations

    Perform complex mathematical operations

  • 2

    Script calculations

    Integrate mathematical operations into shell scripts

  • 3

    Financial calculations

    Perform precise financial and accounting calculations

  • 4

    Scientific computing

    Handle scientific calculations with high precision

Syntax

bc [options] [file...]
bc -l [file...]

Options

Option Description
-h, --help Print a usage message and exit
-i, --interactive Force interactive mode
-l, --mathlib Define the standard math library
-q, --quiet Don't print initial welcome banner
-s, --standard Process exactly the POSIX bc language
-w, --warn Give warnings for extensions to POSIX bc
-v, --version Print version information and exit

Examples

How to Use These Examples

The examples below show common ways to use the bc command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Launch bc interactively
bc
Calculate a simple expression
echo "5 + 3" | bc
Set decimal precision (scale)
echo "scale=4; 10/3" | bc
Calculate exponential expressions
echo "2^10" | bc
Load math library for additional functions
bc -l

Advanced Examples:

Convert from decimal to binary
echo "obase=2; 42" | bc
Convert from binary to decimal
echo "ibase=2; 101010" | bc
Convert from decimal to hexadecimal
echo "obase=16; 255" | bc
Define and use variables
bc << EOF
a = 5 b = 10 a * b EOF
Use conditional statements
bc << EOF
if (10 > 5) { print "True\n" } else { print "False\n" } EOF
Define a function to calculate factorial
bc << EOF
define f(n) { if (n <= 1) return 1 return n * f(n-1) } f(5) EOF
Calculate square root using math library
echo "scale=10; sqrt(2)" | bc -l
Use with loops
bc << EOF
for (i = 1; i <= 5; i++) { i^2 } EOF

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Key Features:

The bc command provides a powerful calculator language with several key features:

  • Arbitrary precision arithmetic (limited only by available memory)
  • Variables and arrays for storing values
  • User-defined functions and loops for complex calculations
  • Conditional execution with if-else statements
  • Base conversion between different number systems
  • Math library with trigonometric and other advanced functions (when using -l)
  • Interactive and script-friendly operation

Common Variables and Settings:

  • scale: Determines the number of decimal places in division operations (default is 0)
  • ibase: Sets the input number base (default is 10 for decimal)
  • obase: Sets the output number base (default is 10 for decimal)
  • last or .: Contains the result of the last printed expression

Math Library Functions (-l option):

  • s(x): sine of x (x in radians)
  • c(x): cosine of x (x in radians)
  • a(x): arctangent of x
  • l(x): natural logarithm of x
  • e(x): exponential function (e^x)
  • j(n,x): Bessel function of integer order n of x
  • sqrt(x): square root of x

Usage Tips:

  • Initialize variables before using them, otherwise they default to 0
  • Array elements are initialized to 0 and auto-expand as needed
  • Create a ~/.bcrc file to set your preferred scale and other settings
  • Use semicolons to separate multiple expressions on one line
  • For complex scripts, use the here document syntax (bc << EOF) for multi-line input

Limitations:

  • No support for direct floating point input notation (like 1.5 - must use 1.5 or set scale)
  • Limited error handling compared to modern programming languages
  • No direct support for complex numbers
  • No built-in random number generation

Common Use Cases

Mathematical calculations

Perform complex mathematical operations

Script calculations

Integrate mathematical operations into shell scripts

Financial calculations

Perform precise financial and accounting calculations

Scientific computing

Handle scientific calculations with high precision

Data processing

Process numerical data with mathematical operations

Related Commands

These commands are frequently used alongside bc or serve similar purposes:

Use Cases

1

Mathematical calculations

Perform complex mathematical operations

2

Script calculations

Integrate mathematical operations into shell scripts

3

Financial calculations

Perform precise financial and accounting calculations

4

Scientific computing

Handle scientific calculations with high precision

5

Data processing

Process numerical data with mathematical operations

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the bc command works in different scenarios.

$ bc
View All Commands