Table of Contents
Using Substitution Syntax
Use Substitution Syntax ($(...)
) to get output from a Python script and store it in a Bash variable.
1 2 3 |
print("Welcome to Java2Blog!") |
1 2 3 4 |
result=$(python my_script.py) echo $result |
1 2 3 |
Welcome to Java2Blog! |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import sys def display_str(string): print(string) if __name__ == "__main__": if len(sys.argv) < 2: print("Please provide a string argument.") else: input_str = sys.argv[1] display_str(input_str) |
1 2 3 4 |
result=$(python my_script_with_args.py "Java2Blog") echo $result |
1 2 3 |
Java2Blog |
Using Backticks
Use backticks to get output from a Python script and store it in a Bash variable.
1 2 3 |
print("Welcome to Java2Blog!") |
1 2 3 4 |
result=`python my_script.py` echo $result |
1 2 3 |
Welcome to Java2Blog! |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import sys def display_str(string): print(string) if __name__ == "__main__": if len(sys.argv) < 2: print("Please provide a string argument.") else: input_str = sys.argv[1] display_str(input_str) |
1 2 3 4 |
result=`python my_script_with_args.py "Java2Blog"` echo $result |
1 2 3 |
Java2Blog |
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.
1 2 3 |
print("Welcome to Java2Blog!") |
1 2 3 4 |
python my_script.py | read result echo $result |
1 2 3 |
Welcome to Java2Blog! |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import sys def display_str(string): print(string) if __name__ == "__main__": if len(sys.argv) < 2: print("Please provide a string argument.") else: input_str = sys.argv[1] display_str(input_str) |
1 2 3 4 |
python my_script_with_args.py "Java2Blog" | read result echo $result |
1 2 3 |
Java2Blog |
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.
1 2 3 |
print("Welcome to Java2Blog!") |
1 2 3 |
python my_script.py | tee |
1 2 3 |
Welcome to Java2Blog! |
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.
1 2 3 |
print("Welcome to Java2Blog!") |
1 2 3 4 |
result=$(python my_script.py | tee) echo $result |
1 2 3 |
Welcome to Java2Blog! |
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.
1 2 3 |
print("Welcome to Java2Blog!") |
1 2 3 |
python my_script.py | tee result.txt |
1 2 3 |
Welcome to Java2Blog! |
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.
1 2 3 |
Welcome to Java2Blog! |
Let’s take the above approach with a different Python script which takes a string argument; see the following example.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import sys def display_str(string): print(string) if __name__ == "__main__": if len(sys.argv) < 2: print("Please provide a string argument.") else: input_str = sys.argv[1] display_str(input_str) |
1 2 3 |
python my_script_with_args.py "Java2Blog" | tee result.txt |
1 2 3 |
Java2Blog |
1 2 3 |
Java2Blog |
Similarly, we can use the
cat
command rather than thetee
command, but thecat
command can’t store the Python script’s output to a given text file and show it on the console simultaneously. You must use thetee
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.
1 2 3 |
print("Welcome to Java2Blog!") |
1 2 3 4 |
python my_script.py > result.txt cat result.txt |
1 2 3 |
Welcome to Java2Blog! |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import sys def display_str(string): print(string) if __name__ == "__main__": if len(sys.argv) < 2: print("Please provide a string argument.") else: input_str = sys.argv[1] display_str(input_str) |
1 2 3 4 |
python my_script_with_args.py "Java2Blog"> result.txt cat result.txt |
1 2 3 |
Java2Blog |
That’s all about how to get output from Python Script in Bash.