How to install the `bc` command in Linux

LinuxLinuxBeginner
Practice Now

Introduction

The bc command is a powerful tool for performing mathematical calculations in Linux. Whether you need to do simple arithmetic or complex calculations, bc provides a command-line calculator that can handle it all. This tutorial will guide you through installing and using the bc command on your Linux system, enabling you to perform calculations directly from your terminal.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/VersionControlandTextEditorsGroup(["Version Control and Text Editors"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/BasicSystemCommandsGroup -.-> linux/bc("Arithmetic Calculations") linux/BasicFileOperationsGroup -.-> linux/chmod("Permission Modifying") linux/FileandDirectoryManagementGroup -.-> linux/which("Command Locating") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") linux/VersionControlandTextEditorsGroup -.-> linux/nano("Simple Text Editing") subgraph Lab Skills linux/bc -.-> lab-414536{{"How to install the `bc` command in Linux"}} linux/chmod -.-> lab-414536{{"How to install the `bc` command in Linux"}} linux/which -.-> lab-414536{{"How to install the `bc` command in Linux"}} linux/apt -.-> lab-414536{{"How to install the `bc` command in Linux"}} linux/nano -.-> lab-414536{{"How to install the `bc` command in Linux"}} end

What is the bc Command?

Before installing bc, let us understand what it is and why it is useful.

The bc command is a command-line calculator utility that provides:

  • Basic arithmetic operations (addition, subtraction, multiplication, division)
  • Advanced mathematical functions (square roots, powers, etc.)
  • Variable support for storing values
  • Control statements for programming
  • Precision control for decimal calculations

Why Use bc?

The bc command is valuable for several reasons:

  1. It allows you to perform calculations without opening a graphical calculator
  2. It can be integrated into shell scripts to automate calculations
  3. It supports arbitrary precision, meaning you can control how many decimal places to display
  4. It provides a programming language for more complex mathematical operations

Let us check if bc is already installed on your system. Open a terminal window and type:

which bc

If bc is installed, this command will display the path to the bc executable. If nothing is displayed, you will need to install it in the next step.

Let us also try to run bc to check if it is available:

bc -v

This will display the version of bc if it is installed. If you see a "command not found" error, you will need to install it.

Installing the bc Command

Now that we understand what bc is, let us install it on our Ubuntu system.

Update Package Repository

First, we need to update the package repository to ensure we get the latest version. Open your terminal and run:

sudo apt update

You will see output similar to this:

Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...
Reading package lists... Done

Install bc

Now, let us install the bc package using the apt package manager:

sudo apt install -y bc

The -y flag automatically answers "yes" to any prompts, making the installation process smoother.

You will see output similar to:

Reading package lists... Done
Building dependency tree... Done
...
Setting up bc (1.07.1-3build1) ...
...

Verify the Installation

After installation, verify that bc is correctly installed by checking its version:

bc --version

You should see output similar to:

bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
...

You can also check the location of the bc executable:

which bc

This should display something like:

/usr/bin/bc

Congratulations! You have successfully installed the bc command on your Ubuntu system.

Basic Usage of the bc Command

Now that you have bc installed, let us learn how to use it for basic calculations.

Starting the bc Interactive Mode

To start bc in interactive mode, simply type bc in your terminal:

bc

You will enter the bc interactive environment, which looks like this:

The empty prompt indicates that bc is ready to accept your calculations. To exit bc at any time, type quit or press Ctrl+D.

Performing Basic Arithmetic

Let us try some basic arithmetic operations in the bc interactive mode:

  1. Addition:

    5 + 3

    Output: 8

  2. Subtraction:

    10 - 4

    Output: 6

  3. Multiplication:

    6 * 7

    Output: 42

  4. Division:

    20 / 4

    Output: 5

By default, bc performs integer division. To see decimal results, you need to set the scale variable, which controls the number of decimal places.

Working with Decimal Places

Set the scale to control decimal precision:

scale=2

Now try a division that results in a decimal:

5 / 2

Output: 2.50

Try another example:

1 / 3

Output: 0.33

If you want more precision, increase the scale value:

scale=10
1 / 3

Output: 0.3333333333

Using bc in One-Liner Commands

You can also use bc directly from the shell without entering interactive mode:

echo "5 + 3" | bc

Output: 8

For calculations with decimals:

echo "scale=2; 5 / 2" | bc

Output: 2.50

This approach is particularly useful in shell scripts or when you need to perform a quick calculation.

Exit the bc Interactive Mode

When you are finished using bc, exit the interactive mode by typing:

quit

Or simply press Ctrl+D.

Advanced bc Command Usage

Now that you are familiar with basic bc usage, let us explore some more advanced features.

Using Mathematical Functions

The bc command supports several mathematical functions. To use these functions, you need to load the math library using the -l option when starting bc:

bc -l

Now you can use various mathematical functions:

  1. Square root:

    sqrt(16)

    Output: 4.00000000000000000000

  2. Sine of an angle (in radians):

    s(3.14159 / 2)

    Output: 1.00000000000000000000

  3. Cosine of an angle:

    c(0)

    Output: 1.00000000000000000000

  4. Natural logarithm:

    l(2.71828)

    Output: 1.00000000000000000000

  5. Exponentiation:

    e(2)

    Output: 7.38905609893065022723

Using Variables

You can use variables to store values and reuse them in calculations:

x = 10
y = 5
x + y

Output: 15

result = x * y
result

Output: 50

Variables make it easier to perform complex calculations or to reuse values.

Creating a Simple Script with bc

Let us create a simple shell script that uses bc to calculate the area of a circle. Open a text editor and create a file named circle_area.sh:

nano circle_area.sh

Add the following content to the file:

#!/bin/bash

## Prompt for the radius
echo "Enter the radius of the circle:"
read radius

## Calculate the area
area=$(echo "scale=2; 3.14159 * $radius * $radius" | bc)

## Display the result
echo "The area of the circle with radius $radius is: $area"

Save the file by pressing Ctrl+O, then Enter, and exit with Ctrl+X.

Make the script executable:

chmod +x circle_area.sh

Now run the script:

./circle_area.sh

Enter a radius when prompted, for example, 5, and you should see a result like:

Enter the radius of the circle:
5
The area of the circle with radius 5 is: 78.53

This script demonstrates how bc can be integrated into shell scripts to perform calculations.

Summary

In this lab, you have learned how to:

  • Understand what the bc command is and why it is useful
  • Install the bc command on your Ubuntu Linux system
  • Perform basic arithmetic operations using bc in interactive mode and from the command line
  • Control decimal precision using the scale variable
  • Use mathematical functions with the math library
  • Work with variables in calculations
  • Integrate bc into shell scripts for automated calculations

The bc command is a powerful tool for performing calculations in Linux, especially when working in the terminal or writing shell scripts. With the knowledge gained from this lab, you can now handle mathematical operations efficiently in your Linux environment.