473,883 Members | 1,809 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using python at the bash shell?

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
16 3960
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

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.o rg/>. 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
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.o rg/>. 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

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
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.o rg/>. 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

"John Salerno" <jo******@NOSPA Mgmail.comwrote in message
news:44******** **************@ news.astraweb.c om...
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
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
>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
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14364
by: Jorgen Grahn | last post by:
I couldn't think of a good solution, and it's hard to Google for... I write python command-line programs under Win2k, and I use the bash shell from Cygwin. I cannot use Cygwin's python package because of a binary module which has to be compiled with Visual C 6. My scripts start with a '#!/usr/bin/env python' shebang, as God intended. Now, I assume I can make cmd.exe run foo.py by asociating *.py with the python interpreter.
6
1859
by: Robin Siebler | last post by:
I have a bunch of command line tests that I need to automate. I've run into too many bugs with the default Win2k command shell, so I need to either purchase 4NT or handle the logic and output processing with Python. I'm looking for experiences, comments, problems, etc. Also, I'm trying to figure out how to use popen(). To say that the documentation and expamples available for this is sparse would be the understatment of the century! ...
14
19848
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
7
2249
by: Frank Potter | last post by:
I learned some python in windows. And now I've turned to linux. I read a book and it teaches how to write shell script with bash, but I don't feel like the grammar of bash. Since I know about python, I want to get a linux shell which use python grammar. I searched by google and I found pysh, which is not maintained any more. There's another script named pyshell, which is not likely what I'm searching for.
9
2505
by: Endless Story | last post by:
My last version of Python was 2.4, running smoothly on XP with path c: \Python24 - no need even to include this path in PATH; everything worked as it's supposed to at the command line. Just installed Python 2.5, after uninstalling 2.4 (and also 2.3 which had lingered). Now if I open a shell in Windows Python is not available! Here are the symptoms: - If I open a shell using "Command line here" with XP Powertools, then enter "python"...
4
7578
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment variable is not in the os.environ dictionary. I know that cron runs in a subshell that does not have all of the normally set environment variables. HOSTNAME is not one of those variables, it is set even in cron's subshell. Why doesn't python get...
0
3490
by: Cameron Simpson | last post by:
On 17Aug2008 21:25, John Nagle <nagle@animats.comwrote: Because $HOSTNAME is a bash specific variable, set by bash but NOT EXPORTED! Like $0 and a bunch of other "private" variables, subprocesses do not inherit this value. From "man bash": Shell Variables The following variables are set by the shell:
8
5591
by: james.kirin39 | last post by:
Hi everyone, After having used Python on Linux for some time, I now have to do Python coding on Windows. I am big fan of the interactive Python shell to test, eg, regexps. Is there an interactive Python shell on Windows that supports: - easy copy-pasting to/from an editor? (as opposed to the cumbersome "mark", "copy" and then "paste" sequence that any terminal on Windows
6
1881
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions I need to write a program that creates real application/FTP accounts
0
11142
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10847
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10416
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7971
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7129
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5797
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5991
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4612
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4220
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.