How to use python -c "code here" with newlines?

Basj

The command

python -c "print('hello')"

runs the code inside the quotes successfully, both in Linux (bash) and Windows (cmd.exe).

But how to pass code with newlines, with python -c?

Example: both

python -c "for i in range(10): if i % 2 == 0: print('hello')"
python -c "for i in range(10):\n    if i % 2 == 0:\n        print('hello')"

fail.


Example use case: I need to send a SSH command (with paramiko) to execute a short Python code on a remote server, so I need to pass one command like
ssh.exec_command('python -c "..."').

Aplet123

You can use bash's $'foo' string syntax to get newlines:

python -c $'for i in range(10):\n if i % 2 == 0:\n  print("hello")'

(I'm using single space indents here)

For windows, you really should be using powershell, which has `n as a newline:

python -c "for i in range(10):`n if i % 2 == 0:`n  print('hello')"

In cmd.exe, it seems that you can use ^ to escape a newline, however I'm unable to test this currently so you should refer to this question's answers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to omit newlines in Python?

From Dev

How to use Linq to Split a String on newlines and space?

From Dev

Is there someway I can use an array or something else to reduce the Code here?

From Dev

Is there someway I can use an array or something else to reduce the Code here?

From Dev

How could I refactor this bit of code here to achieve the same result?

From Dev

How to use the -c flag in python

From Dev

how to use python -c on windows?

From Dev

How do I use File::Slurp to write an array to a file with newlines?

From Dev

How to disable universal newlines in Python 2.7 when using open()

From Dev

How to remove newlines except the ones beside the text in python?

From Dev

How to convert newlines from CRLF to LF while creating Python sdist?

From Dev

How to use awk to use blank lines as record separators and to use newlines as field separators?

From Dev

Python: Configparser - escape newlines?

From Dev

Finding newlines in C++

From Dev

Finding newlines in C++

From Dev

C program printing newlines

From Dev

How to use Cython to compile Python 3 into C

From Dev

How to stop wordpress converting html that is inside <pre><code>here</code></pre>

From Dev

Use sed to fix broken newlines

From Dev

_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by packenter code here

From Dev

_tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by packenter code here

From Dev

How to use grep to get the matching part only, without introducing extra newlines

From Dev

Python string clean up: How can I remove the excess of newlines of a string in python?

From Dev

Unexpected newlines being printed in Python

From Dev

Python Regex Excluding Multiple Newlines

From Dev

Python msgpack module: packb and newlines

From Dev

Python Regex Excluding Multiple Newlines

From Dev

Python regex over multiple newlines

From Dev

Remove whitespace and newlines - beautifulsoup python

Related Related

  1. 1

    How to omit newlines in Python?

  2. 2

    How to use Linq to Split a String on newlines and space?

  3. 3

    Is there someway I can use an array or something else to reduce the Code here?

  4. 4

    Is there someway I can use an array or something else to reduce the Code here?

  5. 5

    How could I refactor this bit of code here to achieve the same result?

  6. 6

    How to use the -c flag in python

  7. 7

    how to use python -c on windows?

  8. 8

    How do I use File::Slurp to write an array to a file with newlines?

  9. 9

    How to disable universal newlines in Python 2.7 when using open()

  10. 10

    How to remove newlines except the ones beside the text in python?

  11. 11

    How to convert newlines from CRLF to LF while creating Python sdist?

  12. 12

    How to use awk to use blank lines as record separators and to use newlines as field separators?

  13. 13

    Python: Configparser - escape newlines?

  14. 14

    Finding newlines in C++

  15. 15

    Finding newlines in C++

  16. 16

    C program printing newlines

  17. 17

    How to use Cython to compile Python 3 into C

  18. 18

    How to stop wordpress converting html that is inside <pre><code>here</code></pre>

  19. 19

    Use sed to fix broken newlines

  20. 20

    _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by packenter code here

  21. 21

    _tkinter.TclError: cannot use geometry manager grid inside . which already has slaves managed by packenter code here

  22. 22

    How to use grep to get the matching part only, without introducing extra newlines

  23. 23

    Python string clean up: How can I remove the excess of newlines of a string in python?

  24. 24

    Unexpected newlines being printed in Python

  25. 25

    Python Regex Excluding Multiple Newlines

  26. 26

    Python msgpack module: packb and newlines

  27. 27

    Python Regex Excluding Multiple Newlines

  28. 28

    Python regex over multiple newlines

  29. 29

    Remove whitespace and newlines - beautifulsoup python

HotTag

Archive