Search code examples
pythonshellinlineexecution

How to execute Python inline from a bash shell


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"'

Solution

  • 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).