Example – any bash keyword (e.g. else or in) will also cause this:
$ python -c 'import sys;for p in sys.path:print p'
File "<string>", line 1
import sys;for p in sys.path:print p
^
SyntaxError: invalid syntax
Double quotes doesn’t fix it.
This has nothing to do with the fact that
foris a Bash keyword; it’s simply thatis not valid Python syntax. You’d get the same error if you ran
pythonwith no arguments and then typed that in at the prompt.You can fix it by adding a newline:
which you can do in your Bash command by writing either this:
or this:
(where
$'...'is a Bash syntax that allows C-like escape sequences in strings, such as\nfor newline).