If you spend most of your time in the command line, you don’t want to leave to do math. Qc is a script that does in-line command line math without forcing you to exit the main bash prompt as you might with a program like
bc or a language interpreter.
python -c "print $1"
Make the script executable with the command:
Alias it to
qc by editing the
.bash_profile file in your home folder and adding the code:
Use qc as follows:
$ qc 7-8
$ qc 9.8-1
$ qc 2**4
In reality, this script is just a glorified alias of
python -c, but importing the math library makes this command considerably more powerful.
python -c "from math import *; print $1"
This wildcard import gives access to constants such as
e in calculations, but more notably, it also allows for the use of functions in calculations. For example:
$ qc "factorial(10)"
$ qc "hex(255)"
$ qc "sin(pi/7)"
Because qc is executed in bash, it requires quotes (single or double) to be placed around expressions with functions, since bash will try to evaluate expressions in parentheses before it does anything else. The quotes tell bash to pass the entire expression as a whole to
qc instead of trying to evaluate part of it beforehand.
Finally, once you have the result of a calculation, the
pbcopy command places the result in the clipboard for ease of use.
# code for OSX
python -c "from math import *; print $1" | pbcopy
pbpaste # show the result in bash
On Linux, a few lines of python will accomplish the same task.
# code for Linux
python -c "from math import *
evaluated = $1
clipboard = gtk.clipboard_get()
clipboard.set_text('%s' % str(evaluated))
Edit 7/23: changed
evaluated in the code to avoid conflict with the bash builtin.
The script determines which copy commands to use based on your OS. The full script is available on Github.