using python at the bash shell?

Simon Forman rogue_pedro at yahoo.com
Tue Aug 15 18:45:33 EDT 2006


cga2000 wrote:
> On Tue, Aug 08, 2006 at 12:22:42PM EDT, Simon Forman wrote:
> > John Salerno wrote:
> > > skip at pobox.com wrote:
> > > >     John> Aside from the normal commands you can use, I was wondering if
> > > >     John> it's possible to use Python from the terminal instead of the
> > > >     John> normal bash commands (e.g. print instead of echo).
> > > >
> > > > Take a look at ipython <http://ipython.scipy.org/>.  It's not precisely what
> > > > you've asked for, but it provides some features that help integrate Python
> > > > with the shell environment.
> > > >
> > > > Skip
> > >
> > > Can you use IPython to do normal bash things, like installing, etc.?
> >
> > "normal bash things"? :-)  Yes, most commands can be run by putting an
> > '!' before them.  If you ever need to run something that for some
> > reason doesn't work with this, you can always run !bash and do it in
> > bash.  :-)
> >
> Sorry, I'm late with my howmework .. but this is rather cool.
>
> Although .. prefixing all your "system commands" with the far-to-reach
> "!" would probably become a royal pain after a while.

I use my computer almost exclusively for programming, so I used xmodmap
to remap the number & symbol keys the other way round, i.e. I get
"!@#$%^&*()" from just pressing the keys and "1234567890" when I press
shift+keys.   It's soooo much nicer (except when I use another
machine...)  So hitting '!' for me at least ain't so bad. ;P   YMMV

I don't actually use Ipython as my shell, but I'd *like* to..

>
> Why do you write "most commands" .. what type of command might not be
> run by "putting '!' before them?"

Well, I wrote that just to hedge my bets, but here's an example:  I set
up lesspipe and source-highlight to add syntax highlighting to various
file types when viewed through less.  But using "!less neat.py" in
Ipython doesn't work because (I'm guessing here) Ipython doesn't handle
the color escape codes.  It winds up looking like this:

ESC[31m#!/usr/local/bin/python2.5ESC[m
ESC[01;34mfromESC[m random ESC[01;34mimportESC[m random as f


ESC[01;34mdefESC[m
ESC[01;30mgESC[mESC[31m(ESC[mESC[31m)ESC[mESC[31m:ESC[m
    n1 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
    ESC[01;34mwhileESC[m TrueESC[31m:ESC[m
        n2 ESC[31m=ESC[m ESC[01;30mfESC[mESC[31m(ESC[mESC[31m)ESC[m
        yield n1 ESC[31m-ESC[m n2
        n1 ESC[31m=ESC[m n2

Ew.

So in this case I'd have to "!bash" then "less neat.py"...

> In the linux world it would be rather interesting if a distro was
> available that uses nothing but python.
>
> The installer .. the startup scripts .. utilities .. etc.
>
> Maybe there is such a thing and I'm just not aware of it?

IMHO it would be neat,  but it'd be kind of a "stunt".  There's a ton
of excellent code in most any linux distro *not* written in python.

> Since it would be byt-code being executed it would be faster than
> regular shell stuff and a lot easier to customize/maintain.

Mmmm...  I bet it'd be hard to beat, say, grep...   Or any of the
small, custom-purpose C-coded tools.   (then there's make... gcc...
not easy to rewrite those..   just off the top of my head...)

>
> Don't know enough to figure out if such a thing is possible, though ..
> 
> Thanks
> 
> cga

Peace,
~Simon




More information about the Python-list mailing list