Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"' 
like image 805
Sean Avatar asked Jun 04 '13 01:06

Sean


People also ask

How do I run a Python code inline?

Using the python Command To run Python scripts with the python command, you need to open a command-line and type in the word python , or python3 if you have both versions, followed by the path to your script, just like this: $ python3 hello.py Hello World!

Can you call a Python script from a bash script?

We can make use of the bash script to run the Python script in macOS/Ubuntu. Both these operating systems support bash scripts. Let's see the steps to run Python scripts using a bash script. Open any text editor.

How do I run a Python module from the shell?

The most basic and easy way to run a Python script is by using the python command. You need to open a command line and type the word python followed by the path to your script file, like this: python first_script.py Hello World! Then you hit the ENTER button from the keyboard and that's it.


1 Answers

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). 
like image 105
Mike Müller Avatar answered Nov 15 '22 13:11

Mike Müller