You want to calculate something with Python quickly, from the command line. You might even want to use Python in a bash script to produce some result.
$ python -c "print 2*3" 6
Storing the result in a variable:
$ X=`python -c "print 2*3"` $ echo $X 6
Thanks to Tajti A. for the tip.
Floating point arithmetic in bash is problematic,
expr supports integers only for instance.
Not an optimal solution but it works:
$ python -c "print 5.5*3" 16.5 $ num=`python -c "print 5.5*3"` $ echo $num 16.5
“IPython is an interactive shell for the Python programming language that offers enhanced introspection, additional shell syntax, syntax highlighting, tab completion and rich history. It is a component of the SciPy package.” (source)
- FrontPage – IPython (the HQ)
- IPython: A System for Interactive Scientific Computing (article, PDF)
- Cookbook – IPython
- IPython mailing list
In the book Python for Unix and Linux System Administration, there is a long chapter (Ch. 2) dedicated to IPython!
Installation and usage
Install it via apt-get:
sudo apt-get install ipython
I made the alias “ip” for the command “ipython”. Note that there is a command “ip” for manipulating routing and tunnels that the alias will hide, but it’s not likely I’ll ever use that command.
The config file of IPython is located at