By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,422 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,422 IT Pros & Developers. It's quick & easy.

using python at the bash shell?

P: n/a
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.
Aug 8 '06 #1
Share this Question
Share on Google+
16 Replies


P: n/a
On Mon, 2006-08-07 at 21:03 -0400, John Salerno wrote:
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".

--
This message has been scanned for viruses and
dangerous content by MailScanner, and is
believed to be clean.

Aug 8 '06 #2

P: n/a

JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal 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
Aug 8 '06 #3

P: n/a
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal 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

Aug 8 '06 #4

P: n/a

John Salerno wrote:
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

Aug 8 '06 #5

P: n/a
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal 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.?
Aug 8 '06 #6

P: n/a

"John Salerno" <jo******@NOSPAMgmail.comwrote in message
news:44**********************@news.astraweb.com...
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
Aug 8 '06 #7

P: n/a
Thomas Bartkus wrote:
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. :)
Aug 8 '06 #8

P: n/a
>I've just reconsiled to pick up my BASH by osmosis and concentrate on
(much!) cleaner scripting with Python.
JohnI don't have much of a need to learn the actual programming parts
Johnof the bash language, ...

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

Skip
Aug 8 '06 #9

P: n/a
sk**@pobox.com wrote:
>I've just reconsiled to pick up my BASH by osmosis and concentrate on
>(much!) cleaner scripting with Python.

JohnI don't have much of a need to learn the actual programming parts
Johnof the bash language, ...

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

Skip
Heh heh, Python all the way!
Aug 8 '06 #10

P: n/a
John Salerno wrote:
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal 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. :-)

Peace,
~Simon

Aug 8 '06 #11

P: n/a
Simon Forman wrote:
"normal bash things"? :-)
forgive my ignorance, i just installed linux on saturday! :)
Aug 8 '06 #12

P: n/a
John Salerno <jo******@NOSPAMgmail.comwrote:
>
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
--
- Tim Roberts, ti**@probo.com
Providenza & Boekelheide, Inc.
Aug 10 '06 #13

P: n/a
On Tue, Aug 08, 2006 at 12:22:42PM EDT, Simon Forman wrote:
John Salerno wrote:
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal 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.

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
Aug 15 '06 #14

P: n/a
cga2000 wrote:
On Tue, Aug 08, 2006 at 12:22:42PM EDT, Simon Forman wrote:
John Salerno wrote:
sk**@pobox.com wrote:
JohnAside from the normal commands you can use, I was wondering if
Johnit's possible to use Python from the terminal instead of the
Johnnormal 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

Aug 15 '06 #15

P: n/a
On Tue, Aug 15, 2006 at 06:45:33PM EDT, Simon Forman wrote:
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"...
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..

:-)
>
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.
quite a large project, I would imagine.
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...)
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
Aug 16 '06 #16

P: n/a
In <ma***************************************@python. org>, cga2000 wrote:
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
Aug 16 '06 #17

This discussion thread is closed

Replies have been disabled for this discussion.