Python as a default shell, replacement of bash, sh, cmd ?

Michael Torrie torriem at gmail.com
Sun Feb 19 19:23:33 EST 2012


On 02/18/2012 11:58 AM, SherjilOzair wrote:
> Has it been considered to add shell features to python, such that it
> can be used as a default shell, as a replacement for bash, etc.
> 
> I'm sure everyone would agree that doing this would make the terminal
> very powerful.
> 
> What are your views on this?

I use python for system programming all the time, in place of bash.
However Python is not designed as shell language, just as Bash is not
really designed for the type of application development that Python is.

Bash works by giving you a view of the file system as your primary way
of interacting with it as a shell.  This is ideal because usually you
want to manipulate files and processes (that's just what you do on a
command line shell normally). Python doesn't work on that level.  Like
other langueages like php, you can manipulate files and processes
through standard library calls.  Frankly doing:

cd ~/blah

is much faster and more convenient in an interactive shell than:

import os
os.chdir(os.getenv("HOME") + "/blah")

Also bash is designed to start and control processes:

ls *.txt | wc -l
or
someprocess | grep something 2>&1 > /tmp/somefile.txt

In python there is no direct correlation to these things, and in fact
Python has different ways of doing that kind of thing (better for some
things) if you want to write scripts (for example,
http://www.dabeaz.com/generators/)

My own experience has shown me that Python is a very capable
systems-programming language, but that traditional shells are there for
a reason.  And if I need to script something, I usually go to Bash first
because it's simpler and easier, at least for very small tasks.  Once
the bash script gets to be 100 lines or more, I switch to python.  Then
I use some modules I wrote myself to make subprocess.Popen a little
simpler.  Another library that I heard about on this list, called
extproc, also seems to be similarly great for doing this.

There have been attempts over the years to bring access to files and
processes into python and still be pythonic, but they are all awkward.
For example, this sort of syntax using dynamic attributes:

shell.wc(shell.ls('/tmp/*.txt'), "-l")
or
shell.ls('/tmp/*.txt').pipe(shell.wc())

But it turns out that coming up with a python-compatible syntax is
pretty hard, especially when it comes to pipes (don't forget standard
err!)  Besides all this, it's just plain awkward and unnecessary.



More information about the Python-list mailing list