Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple CLI Calculator

Setup

This leverages the arbitrary-precision command line calculator bc

Just add the function c (or similarly short) to your .bashrc:

c() { echo "$@" | bc -l; }

Example Usage

With Spaces:

$ c 1 + 1
2

Without spaces

$ c 1+1
2

Exponents

$ c 2^8
256

Multiplication

$ c 8*5*52
2080

Finding pi using the arctangent, a() (there's also sine, s(); and cosine, c())

$ c '4*a(1)'
3.14159265358979323844

Square roots

$ c 'sqrt(2)'
1.41421356237309504880

Decimal to binary conversion

$ c 'obase=2;10'
1010

Binary to decimal conversion

$ c 'ibase=2;10'
2

More complex expressions

$ c '(42*10) ^ 2'
176400
c() { echo "$@" | bc -l; }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment