bc, Shell or cli Calculator

You can do calculations on your CLI/Shell/Terminal itself.
There is a utility called bc.

From man page,:

NAME bc – An arbitrary precision calculator language

SYNTAX bc [ -hlwsqv ] [long-options] [ file … ]

DESCRIPTION bc is a language that supports arbitrary precision numbers with interactive execution of statements. There are some similarities in the syntax to the C programming language. A standard math library is available by command line option. If requested, the math library is defined before processing any files. bc starts by processing code from all the files listed on the command line in the order listed. After all files have been processed, bc reads from the standard input. All code is executed as it is read. (If a file contains a command to halt the processor, bc will never read from the standard input.) This version of bc contains several extensions beyond traditional bc implementations and the POSIX draft standard. Command lineoptions can cause these extensions to print a warning or to be rejected. This document describes the language accepted by this processor. Extensions will be identified as such.

Some simple example :

# 10+20
echo “10+20” | bc

# Convert decimal number 10 to hexadecimal number
echo “obase=16; 10” | bc

# Convert Hexadecimal number 10 to decimal number
echo “ibase=16; 10” | bc

# Convert Binary number 10 to decimal number
echo “ibase=2; 10” | bc

# Convert Decimal number 10 to Binary number
echo “obase=2; 10” | bc

You can also use printf, ofcourse, yes its like C.. 😛 as below

printf ‘%x’ 16


Author: Shingu

Search in pursuit. Help me if you can.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s