DimG
DimG

Reputation: 1781

python multiline command running from bash

I'm trying to run this:

python -c "for i in range(10):\n  print i"

but I get an error:

File "<string>", line 1
for i in range(10):\n  print i
                             ^
SyntaxError: unexpected character after line continuation character

According to this I assume that bash should have processed (namely, newline symbol) command line arguments but the returned error shows the opposite case. Where am I wrong, and why does this happen?

P.S. python-2.7

EDIT

Let me explain my motivation a bit. This code example is definitely pretty silly. Since the doc says that "command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code", I was interested in how should I bring those mentioned newlines to the command properly. The proposed solutions here are:

  1. Use ; to distinguish several commands inside the loop. Yes, that works but it still is a one-liner, I can not use it If I want to run some commands after the loop. ; is not a replacement for a newline.

  2. Type ^M where newline is needed. This hits the goal more precisely but unfortunately, to my point of view, this basically ruins the whole idea of running a python code from the command line because it requires interactive mode. As I understand it's the same as entering a command ant hitting Enter key. So no difference to typing python and working in its shell. That said, I cannot write this in a bash script. Or may I?

Probably the question really should have been splitted into two ones:

  1. Bash escaping:

    Enclosing characters in double quotes (‘"’) preserves the literal value of all characters within the quotes, with the exception of ‘$’, ‘’, ‘\’, and, when history expansion is enabled, ‘!’. The characters ‘$’ and ‘’ retain their special meaning within double quotes (see Shell Expansions). The backslash retains its special meaning only when followed by one of the following characters: ‘$’, ‘`’, ‘"’, ‘\’, or newline.

How does this correspond to the case described? How does bash handles newlines? I found that putting the command into unary quotes makes no change.

  1. How to pass a newline to python in a non-interactive way. (You may say -- why don't you write an ordinary python file with all newlines you want -- you are right but I'm interested in what is exactly meant in the documentation since it quotes newline)

Upvotes: 8

Views: 3247

Answers (3)

damienfrancois
damienfrancois

Reputation: 59090

You actually would need to transform the \n part into an actual newline. That can be done with the $'' syntax:

python -c $'for i in range(10):\n  print i'
0
1
2
3
4
5
6
7
8
9

You can also reach that result with echo -e or printf

$ python -c "$(echo -e "for i in range(10):\n  print i")"

You could also use a here string:

$ python <<< $(echo -e "for i in range(10):\n  print i")

See section 3.1.2.4 ANSI-C Quoting of the Bash Manpage for more information.

Upvotes: 6

ergonaut
ergonaut

Reputation: 7057

You can run a multi-line python -c statement by adding CR characters in your line:

python -c "for i in range(10):^M print (i)^M print ('Hello:' + str(i*i))" 

where ^M is not actually ^ followed by M, it is actually the character you get when you type [CTRL-v][CTRL-m]. Notice the space after this character, which means there are two print statements in the for loop, and it should print:

0
Hello:0
1
Hello:1
....
9
Hello:81

You can do this in a bash script too:

#!/bin/bash
A="python -c \"for i in range(10):^M print (i)^M print ('Hello:' + str(i*i))\""
eval $A

Upvotes: 1

Kalanidhi
Kalanidhi

Reputation: 5092

Remove \n

python -c "for i in range(10):  print i"

Or

You can use ; for using multiple line in for loop

python -c "for i in range(10): print '1st newline';print '2nd newline';print i"

Upvotes: 4

Related Questions