How to execute Python inline from a bash shell

Sean picture Sean · Jun 4, 2013 · Viewed 46.6k times · Source

Is there a Python argument to execute code from the shell without starting up an interactive interpreter or reading from a file? Something similar to:

perl -e 'print "Hi"'

Answer

Mike Müller picture Mike Müller · Jun 4, 2013

This works:

python -c 'print("Hi")'
Hi

From the manual, man python:

   -c command
          Specify  the command to execute (see next section).  This termi-
          nates the option list (following options are passed as arguments
          to the command).