Lesson 70: bc Command
In this lesson, you'll learn how to use the bc command as an interactive calculator and mathematical scripting tool in Linux.
bc (stands for "Basic Calculator") is a command-line utility that offers everything you expect from a simple scientific or financial calculator.
It is a language that supports arbitrary precision numbers with interactive execution of statements, and it has syntax similar to that of the C programming language.
It can be used typically as either a mathematical scripting language or as an interactive mathematical shell.
It comes pre-installed in most Linux distributions. However, if you don't have bc on your system, you can install it using any of the commands given below, according to the package manager for your distribution:
$ sudo apt install bc # Debian/Ubuntu
$ sudo yum install bc # RHEL/CentOS
$ sudo dnf install bc # Fedora 22+
bc Command Syntax
$ bc [OPTIONS]
bc Command Options
| Option | Description |
|---|---|
-l |
Set the default scale to 20 digits and load the standard math library |
-s |
Process exactly the POSIX bc language |
-w |
Enable warnings for extensions to POSIX bc |
-q |
Do not print the normal GNU bc welcome message |
1. Use bc in Interactive Mode
To open bc in interactive mode, type the command bc on the command prompt and simply start calculating your expressions.
$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
10 + 5
15
1000 / 5
200
(2+4) * 2
12
2. Understanding Default Decimal Precision
You should note that while bc can work with arbitrary precision, it actually defaults to zero digits after the decimal point. For example, the expression 3/5 results to 0 as stated below.
$ bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details type `warranty'.
3 / 5
0