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

S

SherjilOzair

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?
 
8

88888 Dihedral

在 2012å¹´2月19日星期日UTC+8上åˆ3æ—¶21分53秒,Jabba Laci写é“:
Have a look at IPython (http://ipython.org/). It can interact with the
normal shell very well.

Laszlo

Yeh, python could be used as a better shell in a text
terminal box and with another text editor in
another box to write scripts in developement.

Also this is easy to test scripts found in the web by copy and paste.
 
B

Bryan

SherjilOzair said:
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 think yes, but rather than become a shell, Python makes easy
programming a shell that can execute Python code. The tendency has
been to excel as a scripting language, and prefer to add extra
features to the standard library.

To start, look at the built-in functions eval() and (raw_)input; and
the library modules: subprocess, os and os.path, glob and re, shutil,
and code.
I'm sure everyone would agree that doing this
would make the terminal very powerful.

'Cept of course that they already are.
 
S

SherjilOzair

Well, if not modify python itself, I was thinking of making another shell, which borrows a lot from python, something like merging bash and python. such that I can do `cd ~/Desktop/dev` and `for i in open('file.txt'): print i` at the some shell. This I think would be VERY useful.

IPyhton is very good, but after all, it is just an advanced interpreter, not a default shell. I don't want this to run on top of bash or sh. But it should run on its own, at shell level.

Bash and sh, according to me, have very ugly syntaxes and the general user does not even use those. Python put on the shell would be adhering to python's vision, i.e. bringing programming to the masses.
The general user, who earlier could not do batch operations, and had to buysoftware and such for all that, could now write his open simple python script and run it in his shell that would do as he wants.

Python on the shell could effectively remove learning grep, awk, sed, bash and the various unix utilities.
Don't take me wrong. Those are awesome tools, and I use them. But the awesomeness is not experienced by the general UNIX user on mac or linux. Python could do that.

We all know how great a programming language python is. Imagine being able to control your computer with such an elegant language. Imagine that I import some speech recognition utility on the terminal shell, and voila, I'm speaking to the computer and it is doing stuff on the terminal for me.

Shell would give python raw power! And Python would manage it well.
 
S

SherjilOzair

Well, if not modify python itself, I was thinking of making another shell, which borrows a lot from python, something like merging bash and python. such that I can do `cd ~/Desktop/dev` and `for i in open('file.txt'): print i` at the some shell. This I think would be VERY useful.

IPyhton is very good, but after all, it is just an advanced interpreter, not a default shell. I don't want this to run on top of bash or sh. But it should run on its own, at shell level.

Bash and sh, according to me, have very ugly syntaxes and the general user does not even use those. Python put on the shell would be adhering to python's vision, i.e. bringing programming to the masses.
The general user, who earlier could not do batch operations, and had to buysoftware and such for all that, could now write his open simple python script and run it in his shell that would do as he wants.

Python on the shell could effectively remove learning grep, awk, sed, bash and the various unix utilities.
Don't take me wrong. Those are awesome tools, and I use them. But the awesomeness is not experienced by the general UNIX user on mac or linux. Python could do that.

We all know how great a programming language python is. Imagine being able to control your computer with such an elegant language. Imagine that I import some speech recognition utility on the terminal shell, and voila, I'm speaking to the computer and it is doing stuff on the terminal for me.

Shell would give python raw power! And Python would manage it well.
 
S

Steven D'Aprano

Well, if not modify python itself, I was thinking of making another
shell, which borrows a lot from python, something like merging bash and
python. such that I can do `cd ~/Desktop/dev` and `for i in
open('file.txt'): print i` at the some shell. This I think would be VERY
useful.

IPyhton is very good, but after all, it is just an advanced interpreter,
not a default shell. I don't want this to run on top of bash or sh. But
it should run on its own, at shell level.

That's up to your operating system. If your OS lets you choose a shell,
tell it to use IPython. IPython already supports being used as the system
shell:

http://ipython.org/ipython-doc/dev/interactive/shell.html
http://transneptune.net/2009/06/16/ipython-as-your-default-shell/

If your OS doesn't support choosing a shell, you can't use anything but
the built-in shell regardless of what Python does.

Either way, this is not a Python problem to solve. It is an OS issue.
 
T

Terry Reedy

Well, if not modify python itself, I was thinking of making another
shell, which borrows a lot from python, something like merging bash
and python. such that I can do `cd ~/Desktop/dev` and `for i in

'cd xxx' cannot work because that is not python syntax. "cd('xxx')"
could. Right now, one can import os and do shell stuff, but not so
convinient for interactive use. 'os.chdir' is not so convenient as 'cd'.
Two possible options, either of which might exist on PyPI:

1. a shell module used as 'from bashshell import *' which would have
functions closely mimicking, in this example, bash

2. a shell module used as 'from bashshell import bash; bash()' which
would invoke an sub-interactive mode like help() that would allow 'cd
xxx' and similar syntax, which still affecting the global environment.
The latter would trade the inconvenience of '()'s for the inconvenience
of entering and exiting a special submode.

I have not used IPYthon so I have no idea how close it gets to either of
these.
 
M

Michael Torrie

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.
 
8

88888 Dihedral

在 2012å¹´2月20日星期一UTC+8上åˆ8æ—¶23分33秒,Michael Torrie写é“:
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.
To use Python as a shell with customized scripts is feasiable for those hose have to work on different OS enviroments from time to time. This is a handy approach to ease the user the burden to memorize
different commands of different OS environments.

Of course, this might add some overheads in executing some operations.

Nowadays attracting more people especially noices to use linux is the way to keep unix programming alive in the future.
 
S

Serhiy Storchaka

18.02.12 20:58, SherjilOzair напиÑав(ла):
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?

Look at Hotwire (http://code.google.com/p/hotwire-shell/).
 
K

Kyle T. Jones

Well, if not modify python itself, I was thinking of making another shell, which borrows a lot from python, something like merging bash and python. such that I can do `cd ~/Desktop/dev` and `for i in open('file.txt'): print i` at the some shell. This I think would be VERY useful.

That's an awful lot of key mashing just to replace 'cat file.txt'

for i in big small upper lower ; do for j in conf bin log ; do chmod
2755 /home/me/$i/$j ; done ; done

cat data.log | grep Error | awk -F, '{print $5, $1, $2,}' | sort

??

I believe your solution seeks a problem. I also believe a tool that
seeks to be all things generally does none of them particularly well.

Cheers.
 
G

Grant Edwards

That's an awful lot of key mashing just to replace 'cat file.txt'

No kidding. And how much python would it take to do "less file.txt"?
for i in big small upper lower ; do for j in conf bin log ; do chmod
2755 /home/me/$i/$j ; done ; done

chmod 2755 /home/me/{big,small,upper,lower}/{conf,bin,log}
cat data.log | grep Error | awk -F, '{print $5, $1, $2,}' | sort

awk -F, '/Error/ {print $5, $1, $2}' data.log | sort

;)

Any decent modern shell (e.g. Bash) is a stunningly powerful
environment aimed at solving a certain sort of problems in a certain
way. Python is aimed at solving a more general set of problems in an
entirely different way.

Yes, you can drive a nail with a screwdriver if you try hard enough
long enough, but it's still a bad idea.
 
S

Steven D'Aprano

No kidding. And how much python would it take to do "less file.txt"?

A lot less than the amount of C it takes to do "less file.txt".

I trust you don't imagine that a Python shell would require the user to
re-implement "less" using nothing but built-ins each time they needed it.
There would be a pre-made "less" replacement (which may even be a wrapper
around the system "less") never more than one import away. You can
already do something very close to this right now:

from pydoc import pager
pager(open("file.txt").read())

How much effort would it take to turn pydoc.pager into a full replacement
for "less"? I don't know, but it would only need to be done ONCE.

This is not a theoretical argument. IPython already does this. It not
only gives you full access to all your shell commands, plus more, but it
let's you use something very close to shell syntax to do it.

http://ipython.org/ipython-doc/dev/interactive/shell.html

The advantage of a Python shell like IPython is not for trivial shell
commands like "less file.txt". I can think of at least four reasons why
you might consider a Python shell would be awesome:

"I know Python, and I don't know bash. What do I care if it takes five
lines to perform a task in Python, vs one line in bash, if it would take
me half an hour of reading man pages and seven attempts to get the bash
version right?"

"I'm fed up to the back teeth of the shell's cryptic syntax, magic global
variables and especially its arcane string quoting rules."

"I'm sick and tired of having to parse human-readable presentation text
to get the answers I want. I want an environment where the tools don't
mix content and presentation by default."

"My OS has a shell which sucks and blows at the same time. I want
something better."

So basically, there exists a niche for Python as a shell, and thanks to
the time machine, there already exists a powerful Python shell ready to
fill that niche.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top