Updated: 28 September 2026 · Applies to: GNU bc 1.07 on AlmaLinux and Rocky Linux 9 and 10, RHEL 9 and 10, Ubuntu 24.04 and 26.04, Debian 13

bc is a calculator for the command line with arbitrary precision. It is handy in shell scripts, which can only do whole-number arithmetic on their own, and for quick sums while you work on a server. We tested every example below with bc 1.07.1 on AlmaLinux 9.

Install

dnf install bc      # AlmaLinux, Rocky Linux, RHEL
apt install bc      # Ubuntu, Debian

Quick calculations

echo "2^10" | bc                 # 1024
echo "scale=4; 10/3" | bc        # 3.3333
echo "(512*1024)/60" | bc        # whole-number result

By default bc divides without decimals. scale sets the number of decimal places.

The math library

echo "s(1)" | bc -l              # sine of 1 radian: .84147098480789650665
echo "sqrt(2)" | bc -l
echo "l(100)/l(10)" | bc -l      # logarithm base 10 of 100

-l loads the math library (sine s, cosine c, arctangent a, natural logarithm l, exponent e) and sets scale=20.

Number bases

echo "obase=2; 42" | bc          # 101010 (decimal to binary)
echo "ibase=16; FF" | bc         # 255 (hexadecimal to decimal)
echo "obase=16; 255" | bc        # FF

Hexadecimal digits must be upper case. Set obase before ibase when you use both, because values after ibase are read in the new base.

In scripts

used=$(df --output=used -k / | tail -1)
total=$(df --output=size -k / | tail -1)
echo "scale=1; $used*100/$total" | bc

This prints the percentage of the root file system in use, with one decimal.

Interactive use

Run bc -l without input to get a calculator prompt. Type expressions and press Enter; quit leaves. The variable last holds the previous result.

Official documentation: GNU bc manual.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket