Command Line Calculator

I can usually type faster in the terminal than doing mouse click on a gui calculator, so I created this scipt to be able to do it quickly from the terminal. There are alot of command line calculators out there so use the one you are comfortable with but I like using bc because of the syntax. For example, you can type:

calc "6/(3*10)"

or something more complex:

calc "8^2*(10/2+(13.2032565*2030)/.349548)" 100

100 is optional, it will specify how many decimals you want to carry it out to (the default is 4).

#!/bin/sh
# Command line calculator
# Display usage if no parameters given
if [ -z "$@" ]; then
echo " ${0##*/} <input> <*decimals> - command line calculator (-h for examples)"
exit
fi
# Decimal to be carried out to (uses four unless value is given)
if [ -z "$2" ]; then
decimals=4; else
decimals=$2
fi
case $1 in
-h | --help ) echo " Requires quotes. Examples:"
echo ' calc "5*((2^3*2)/1.352)"'
echo ' calc "p=10; n=9; p*n+10"' ;;
* ) echo "scale=$decimals;$1" | bc
esac
view raw calc hosted with ❤ by GitHub

2 thoughts on “Command Line Calculator

  1. livinlavidacli

    I’m going to link to this post on my blog, I’ve been looking for a decent command line calculator to review and this looks great

    Reply

Leave a comment