Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python multiline command running from bash

Tags:

python

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)
like image 528
DimG Avatar asked Oct 27 '15 13:10

DimG


People also ask

How to use multiline statements in Python command line?

There are multiple ways in which you can use multiline statements in the command line in python. For example, bash supports multiline statements, which you can use like: $ python -c ' > a = True > if a: > print("a is true") > '. This will give the output: a is true.

How to run Python commands from a bash script?

I can't remember how I fixed it that time. To run a set of Python commands from a bash script, you must give the Python interpreter the commands to run, either from a file (Python script) that you create in the script, as in

How to run multi-line shell code in Linux?

Finally, the best technique for running multi-line shell code is scripting. Using shell scripts, we can edit and adjust our commands without having to rewrite them. Also, they allow us to use the “write-once, run-many” principle for repetitive tasks.

Is it possible to run multiple commands from a single command line?

Single-line command statements are the norm on Linux command lines. However, sometimes we may need, or simply find it efficient, to run multiple commands from the prompt. In this tutorial, we’ll look at various methods for running multi-line commands from a single command-line prompt. 2. Multi-Line Code Techniques


3 Answers

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.

like image 186
damienfrancois Avatar answered Oct 12 '22 05:10

damienfrancois


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"
like image 43
Kalanidhi Avatar answered Oct 12 '22 04:10

Kalanidhi


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
like image 30
ergonaut Avatar answered Oct 12 '22 05:10

ergonaut