6

I would like to use python for things I've been doing using bash. Is it possible to use the -c switch for long programs, e.g. a for loop with two statements? This would let me use python directly from command line, just like bash or php.

Thanks.

EDIT: Don't know how I missed it, simply doing a python -c ' and then pressing enter does what I've wanted to do. I'd tried a lot of variations, and one using a \ but that didn't work, so I asked the question. e.g.

$python -c '
>print "x"
>for i in range(3):
>   print "y" '

does what I wanted to do, though Rod's answer looks good too.

0fnt
  • 8,211
  • 9
  • 45
  • 62

4 Answers4

8

No problem if your underlying shell is bash, since you can continue an argument across multiple lines if an opened ' (quote) is not yet closed -- e.g.:

$ python -c'for x in range(3):
>   if x!=1:
>     print x'
0
2
$

The > is bash's default PS2, the "multi-line continuation prompt", as distinguished from $, AKA PS1, the normal "start entering a command" prompt.

If you can't use such multi-line continuation, multiple nested block statements (such as an if within a loop) could otherwise be problematic.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • It's probably worth noting that this does *not* work in Windows. Ah the glory of cmd.exe. – dash-tom-bang Sep 03 '10 at 17:45
  • @dash, well it does work in windows _with bash_ (e.g. via cygwin) or other good/advanced shells: as I said, it's a shell issue, and sufficiently poor shells (such as the one bundled in cmd.exe) just can't cut it;-). – Alex Martelli Sep 03 '10 at 20:47
5

You can use compound statements, using the semi-colon to delimiter the statements, such as

python -c "for x in range(0,3) : print x; print x

Then output would then be:

0
0
1
1
2
2

see http://docs.python.org/reference/compound_stmts.html

Rod
  • 52,748
  • 3
  • 38
  • 55
3

When used inside a script, I think it would be better to have python read the script from standard input, like so:

#!/bin/bash

python - arg1 arg2 <<END
import sys
print 'Arg:', sys.argv[1:]
END

This uses bash's HEREDOC syntax.

loevborg
  • 1,774
  • 13
  • 18
  • 1
    Good approach but be careful of subtleties in heredoc syntax. (1) Use single quotes around the sentinel word, otherwise contents of the block that happen to match Unix environment variables will be expanded -- single quotes treats the block text as literal; and (2) be sure to use a sentinel word that can't occur as a legal line in the text you are wrapping, else you could have an early false stop. For that reason I generally use <<<'---END---' at the top, and ---END--- at the bottom. Hard to get tripped up this way. – Chris Johnson May 10 '13 at 17:10
1

If you are running from a bash script, just use quotes:

#!/bin/sh

python -c 'import os
for i in range(3):
    for j in range(3):
        print i*j
'

echo "done"

Otherwise, if using the cmd line, use ; semicolons to seperate statements, or use single quotes again to wrap around to the next line:

python -c 'import os
>    for i in range(3):
>        for j in range(3):
>            print i*j
>    '
zdav
  • 2,752
  • 17
  • 15