Get Output from Python Script in Bash

Bash get output from python script

Using Substitution Syntax

Use Substitution Syntax ($(...)) to get output from a Python script and store it in a Bash variable.

We used the command substitution syntax,$(command), to capture the output of the Python script (my_script.py) in a Bash variable (result). Here, the command was python my_script.py, where a Python interpreter executed the script. Finally, we used an echo to display the value of the result variable on the Bash console.

Let’s learn another example where the Python script takes a string value as an argument.

Using Backticks

Use backticks to get output from a Python script and store it in a Bash variable.

This snippet is similar to the last example, but we wrapped the python my_script.py command within the backticks rather than enclosing it within the substitution syntax ($(...)).

Will this approach work if the Python script takes a string value as an argument? Let’s learn it below.

Using Pipe

We can use a pipe to get output from the Python script to do various things with that output. Let’s learn it below.

Use Pipe with read Command

Use a pipe with the read command to get output from a Python script in Bash variable.

In the above example, we used a Python interpreter (python) to execute the Python script and redirected its output to the read command via a pipe. The read command read the received content and stored it in the result variable, which was further used with the echo command to display it on the standard output (Bash console).

Let’s have another example where the Python script needs a string value that must be passed as an argument.

Use Pipe with tee Command

Use a pipe with the tee command to get output from a Python script on the Bash console without storing it in any variable or file.

Again, we used the Python interpreter to run the script and redirected its output to the tee via a pipe. Then, the tee command displayed the received input on the console.

Note that the tee command in Bash reads standard input and writes it to both standard output and one or multiple files. It is usually part of a pipeline, and any number of commands can precede or follow it.

In the pipeline, the previous command’s output is the following command’s input. Remember, a pipeline is processed from left to right.

Use a pipe with the tee command to get output from a Python script and store it in a variable.

We used the substitution syntax to capture the output of the entire pipeline (python my_script | tee) and stored it in the result variable.

Are you looking for a solution to store the outcome of the Python script in the specified text file and print it on the console? You can do it by specifying the text file name preceded by the tee command, as demonstrated below.

Now, open the result.txt file to see its content or use the cat result.txt command to see the following output on the Bash console.

Let’s take the above approach with a different Python script which takes a string argument; see the following example.

Similarly, we can use the cat command rather than the tee command, but the cat command can’t store the Python script’s output to a given text file and show it on the console simultaneously. You must use the tee command in that particular use case.

Using Redirection Operator

Use the redirection operator to get the output of a Python script in a text file. This approach is useful when you neither want to display the Python script’s output on the Bash console nor store it in a variable but write to a text file.

In the above example, we used the Python interpreter to run the Python script and redirected its output to the result.txt file using the redirection operator (>). Then, we used the cat command to read and display the content of the result.txt file on the Bash console.

Let’s see another example where a Python script takes a string-type argument.

That’s all about how to get output from Python Script in Bash.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *