Why do I receive a syntax error for the following one liner python code?
python -c \'import re; if True: print \"HELLO\";\'
File \"\", line 1
One option to work around this limitation is to specify the command with the $'string'
format using the newline escape sequence \n
.
python -c $'import re\nif True: print "HELLO";'
Note: this is supported by shells such as bash and zsh, but is not valid POSIX sh.
As mentioned by @slaadvak, there are some other workarounds here: Executing Python multi-line statements in the one-line command-line