[Tutor] Running Python from UNIX

Magnus Lycka magnus@thinkware.se
Wed, 07 Aug 2002 11:18:14 +0200


At 09:21 2002-08-07 +0530, Anand Ramakrishna wrote:
>Hello,
>           I wanted to run a single line script from the command line. I=20
> typed from the UNIX command prompt  python -c print 'Hello World'  . This=
=20
> did not give any error message. It got executed but did not print out the=
=20
> result. Could anyone tell me what is happening. I did not install python=
=20
> in /usr/bin like others. It is installed in my home directory. In my=20
> .bashrc file I have aliased python to the python executable that gets=20
> created after the istallation. Could this be a problem ?

What you are running is actually equivalent to

$ python -c print

You will print a newline. Compare with

$ python -c pass

Notice the difference?

The '-c' flag means that _the_next_parameter_ will be regarded as a
python command, which is sent to the python interpreter. Not the
entire rest of the line. What you want is to make the entire
"print 'Hello World'" the next parameter. For that you need to type :

$ python -c "print 'Hello World'"

or

$ python -c 'print "Hello World"'

The second version doesn't work with Windows 2000 command prompt (cmd.exe).
I haven't checked other MS OSes. Both works in bash at least.

This isn't really a python issue but a general unix shell thing.
The unix shell is powerful, but there is a learning curve... (Python
is more powerful, and has less learning curve. :-)


--=20
Magnus Lyck=E5, Thinkware AB
=C4lvans v=E4g 99, SE-907 50 UME=C5
tel: 070-582 80 65, fax: 070-612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se