using python at the bash shell?

J

John Salerno

Hi all. I just installed Ubuntu and I'm learning how to use the bash
shell. Aside from the normal commands you can use, I was wondering if
it's possible to use Python from the terminal instead of the normal bash
commands (e.g. print instead of echo). My main reason for asking is that
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.

Thanks.
 
J

John McMonagle

Hi all. I just installed Ubuntu and I'm learning how to use the bash
shell. Aside from the normal commands you can use, I was wondering if
it's possible to use Python from the terminal instead of the normal bash
commands (e.g. print instead of echo).
From a terminal window typing,

python -c "print 'testing'"

would produce the same results as

echo 'testing'

in bash shell, but obviously not as nice.
My main reason for asking is that
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.

Just type python and do your stuff in the python interpreter. This
becomes your shell.

This then got me thinking, how about making python your login shell. So
I modified my shell login entry in /etc/passwd to /usr/bin/python,
logged in and voila, I was presented with my friendly python shell upon
opening a terminal window.

So the answer is "Yes".
 
S

skip

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
 
S

Simon Forman

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

FWIW, I second this. If you want to use python as your shell use
Ipython. It's just great. Fernando Pérez has done a Good Thing IMHO.

Be sure to read (or at least skim) the manual:
http://ipython.scipy.org/doc/manual/index.html

Here's an example of calling "man ls" and then getting the results of
"ls -l" into a python list (one list item per line) and then extracting
the size (as a string) of the MANIFEST file... Enjoy... (I hope
reformatting doesn't screw this up too badly.)


$ ipython
Python 2.4.3 (#2, Apr 27 2006, 14:43:58)
Type "copyright", "credits" or "license" for more information.

IPython 0.7.1.fix1 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

In [1]: !man ls
Reformatting ls(1), please wait...
<viewing actual man page here. ~Simon>

In [2]: !!ls -l
Out[2]:
['total 40',
'drwxr-xr-x 3 sforman sforman 4096 2006-08-02 12:41 docs',
'drwxr-xr-x 3 sforman sforman 4096 2006-04-27 23:49 logs',
'-rw-r--r-- 1 sforman sforman 887 2006-04-27 23:49 MANIFEST',
'-rw-r--r-- 1 sforman sforman 173 2006-05-04 14:04 MANIFEST.in',
'-rw-r--r-- 1 sforman sforman 3338 2006-06-14 20:19 README',
'-rw-r--r-- 1 sforman sforman 475 2006-07-16 17:40 setup.py',
'drwxr-xr-x 4 sforman sforman 4096 2006-08-07 15:04 src',
'drwxr-xr-x 3 sforman sforman 4096 2006-06-21 20:59 tests',
'-rw-r--r-- 1 sforman sforman 898 2006-07-09 21:53 TODO',
'drwxr-xr-x 3 sforman sforman 4096 2006-08-05 17:37 util']

In [3]: data = [n.split() for n in _[1:]]

In [4]: print data[2][4]
887

In [5]:


etc...

HTH,
~Simon
 
B

BartlebyScrivener

John said:
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.

And like vim, Ipython works on both windows and ubuntu.

rd
 
J

John Salerno

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.?
 
T

Thomas Bartkus

John Salerno said:
Hi all. I just installed Ubuntu and I'm learning how to use the bash
shell. Aside from the normal commands you can use, I was wondering if
it's possible to use Python from the terminal instead of the normal bash
commands (e.g. print instead of echo). My main reason for asking is that
I like using Python for everything, and if I don't need to learn the
bash 'language', then I won't just yet.

The answer is yes and you are getting excellent tips from others.

I am just validating your experience by saying that coming from Python is a
barrier to my learning BASH. The more I work with Linux/BASH, the more I
see how I might have used BASH to script something I have already done in
Python. But the question that always comes up is why bother with BASH at
all ;-) And with Python available everywhere, ....

I've just reconsiled to pick up my BASH by osmosis and concentrate on
(much!) cleaner scripting with Python.

Thomas Bartkus
 
J

John Salerno

Thomas said:
I am just validating your experience by saying that coming from Python is a
barrier to my learning BASH. The more I work with Linux/BASH, the more I
see how I might have used BASH to script something I have already done in
Python. But the question that always comes up is why bother with BASH at
all ;-) And with Python available everywhere, ....

I've just reconsiled to pick up my BASH by osmosis and concentrate on
(much!) cleaner scripting with Python.

Glad to know I'm not alone! But I'm certainly much happier using Python
than bash anyway. All I really want to learn as far as the shell is
concerned are the 'natural' things you can do, such as directory/file
manipulation, installing programs, etc. I don't have much of a need to
learn the actual programming parts of the bash language, especially when
I can do the same with Python, which is so much cooler. :)
 
S

skip

John> I don't have much of a need to learn the actual programming parts
John> of the bash language, ...

As long as you promise never, ever, ever to try programming in csh...

Skip
 
J

John Salerno

John> I don't have much of a need to learn the actual programming parts
John> of the bash language, ...

As long as you promise never, ever, ever to try programming in csh...

Skip

Heh heh, Python all the way!
 
S

Simon Forman

John said:
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. :)

Peace,
~Simon
 
T

Tim Roberts

John Salerno said:
Can you use IPython to do normal bash things, like installing, etc.?

Most scripts on Linux have a "hash-bang" line as their first line:
#! /bin/sh

When you execute that script, the system knows that it has to load sh or
bash to process it, regardless of what program launched the script. The
same thing works for Python scripts:
#! /usr/bin/python
 
C

cga2000

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

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

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?

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

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

Thanks

cga
 
S

Simon Forman

cga2000 said:
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"...
[QUOTE]
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?[/QUOTE]

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
 
C

cga2000

cga2000 wrote: [..]
> 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"...[/QUOTE]

you could use "vim -R" -- or its alter ego "view" ..

"!view neat.py"

I tried that just now under ipython and I end up with the exact same
syntax highlighting that I have when I run vim from the bash prompt.

The problems I am having are mainly due to my bash customization --
aliases, functions .. all that stuff is no longer being recognized ..
and also some bash built-ins such as "cd" do not do anything .. maybe
ipython starts a subprocess that switches and then immediately exits ..
so when I get the prompt back I'm back where I started.

I guess reading the ipython manual and trying to understand what I'm
doing before going any further wouldn't hurt..

:)
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.
quite a large project, I would imagine.
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...)

Sorry .. couldn't think of a better word .. but by "utilities" I meant
all those shell scripts in /usr/bin that come with gnu/linux distros and
mostly appear to front-end "real" programs.

I guess a python "system shell" could invoke all the C-coded stuff the
same way bash does, no..?

'nuff [OT] for now..

Thanks,

cga
 
M

Marc 'BlackJack' Rintsch

and also some bash built-ins such as "cd" do not do anything .. maybe
ipython starts a subprocess that switches and then immediately exits ..
so when I get the prompt back I'm back where I started.

If you put a ``!`` in front of ``cd`` this is what happens. Just leave
out the ``!``. IPython has it's own built-in ``cd`` command.

Ciao,
Marc 'BlackJack' Rintsch
 

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,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top