Python multi-line bash command
I am 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 handled (namely, a newline) command line arguments, but the error returned shows the opposite. Where am I mistaken and why is this happening?
PS Python-2.7
EDIT
Let me explain my motivation a bit. This sample code is definitely pretty dumb. Since the document says that " command can be one or more statements, separated by newlines, with significant leading spaces, as in normal modular code," I was interested in how to properly bring those mentioned newlines to command . Suggested solutions here:
Use
;to distinguish several commands within a loop. Yes, it works, but it is still single-line, I cannot use it. If I want to run some commands after a loop.;not a newline replacement.Enter
^Mwhere a new line is required. This more closely matches the goal, but, unfortunately, from my point of view, it basically destroys the whole idea of ββrunning python code from the command line, because it requires interactive mode. As I understand it, this is the same as entering the ant command by pressingEnter. Therefore, there is no difference inpythoninput and work in its shell. However, I cannot write this in a bash script. Or can I?
Probably the question really should have been divided into two:
- Bash shielding:
Double-quoted closing characters ('") keep the literal value of all characters in quotation marks, except for' $, '
', '\', and, when history expansion is enabled, '!'. The characters '$' and 'retain their the special meaning in double quotes (see the "Shell Extensions" section). its special meaning, followed by only one of the following characters: '$,' `, '', '\ or newline.
How does this fit the case described? How does bash handle newlines? I found that entering the command in unary quotation marks does not make any changes.
- How to pass a new line in python in a non-interactive way. (You can say - why don't you write a regular python file with all the new lines you want - you're right, but I'm interested in what exactly is meant in the documentation, as it quotes a new line).
You really need to convert the \n part to the actual newline. This can be done using 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 achieve this result with echo -e or printf
$ python -c "$(echo -e "for i in range(10):\n print i")" You can also use the following line:
$ python <<< $(echo -e "for i in range(10):\n print i") See section 3.1.2.4 Quote ANSI-C Bash Manpage for more information.
You can run the multi-line python -c statement by adding CR characters to your line:
python -c "for i in range(10):^M print (i)^M print ('Hello:' + str(i*i))" where ^ M doesn't really follow ^, followed by M, it's actually the character you get when you enter [CTRL-v] [CTRL-m]. Note the space after this character, which means that 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