Skip to content

Instantly share code, notes, and snippets.

@kallmanation
Last active April 20, 2023 11:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kallmanation/fa535dc9da0cdadaaaf7c9d2630e5e99 to your computer and use it in GitHub Desktop.
Save kallmanation/fa535dc9da0cdadaaaf7c9d2630e5e99 to your computer and use it in GitHub Desktop.
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