Use the subprocess module instead:

import subprocess
output = subprocess.check_output("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'", shell=True)

Edit: this is new in Python 2.7. In earlier versions this should work (with the command rewritten as shown below):

import subprocess
output = subprocess.Popen(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'], stdout=subprocess.PIPE).communicate()[0]

As a side note, you can rewrite

cat syscall_list.txt | grep f89e7000

To

grep f89e7000 syscall_list.txt

And you can even replace the entire statement with a single awk script:

awk '/f89e7000/ {print $2}' syscall_list.txt

Leading to:

import subprocess
output = subprocess.check_output(['awk', '/f89e7000/ {print $2}', 'syscall_list.txt'])
Answer from Rob Wouters on Stack Overflow
🌐
Talkerscode
https://talkerscode.com › howto › python-shell-command-output-to-variable.php
Python Shell Command Output To Variable - TalkersCode.com
July 1, 2023 - Python provides a “subprocess.check_output()” function that you can use in your program to get the output of a shell command into a variable.
Discussions

bash - Python output to a shell variable - Unix & Linux Stack Exchange
It sounds like you're asking How can I assign the output of a command to a shell variable? - however, be aware that bash itself can really only perform integer arithmetic ... For comparing fractional numbers, you'll need a different tool, like bc: x=$(python code.py); if (( $(echo "$x <= 1.5" ... More on unix.stackexchange.com
🌐 unix.stackexchange.com
November 11, 2019
http redirect - Any way to assign terminal output to variable with python? - Stack Overflow
I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in pyth... More on stackoverflow.com
🌐 stackoverflow.com
redirecting python output to bash variables
output=($(python3 draft.py)) echo "${output[0]}" # should be your number echo "${output[1]}" # and the link the extra parentheses make it an array, each value in the array separated by whitespace (so strings that have whitespace would need to be quoted) More on reddit.com
🌐 r/bash
5
4
May 20, 2018
Use python to execute shell command and redirect the output to a variable - Stack Overflow
I am trying to do 2 things with Python- Execute a command at the terminal Save the output of the above command to a variable. This is my code till now - import subprocess ip_address = "http:// More on stackoverflow.com
🌐 stackoverflow.com
🌐
Medium
https://medium.com › @sumitdhattarwal4444 › how-to-store-the-output-of-a-command-in-a-variable-using-python-cd47fcd88845
How to store the output of a command in a variable using Python | by Sumit Dhattarwal | Medium
June 30, 2023 - Using the subprocess Module: The ... To store the output of a command in a variable, we can leverage the subprocess module's run() or check_output() functions....
🌐
Stack Exchange
https://unix.stackexchange.com › questions › 551653 › python-output-to-a-shell-variable
bash - Python output to a shell variable - Unix & Linux Stack Exchange
November 11, 2019 - Are you certain? I just tried the following and it works fine for me: x=$(python3 -c 'import sys ; print(sys.version[0:3])'); echo $x ... @RAJENDRATHAPA The command substitution (the $(...) bit) in that comment should contain the shell code necessary to invoke your Python script.
🌐
Medium
https://medium.com › @vanshikar › assigning-shell-command-output-to-python-variable-88672ef16af3
Assigning Shell Command Output to Python Variable - Vanshika Rangwani - Medium
July 18, 2023 - import subprocess # Execute the command and capture the output output = subprocess.check_output("date", shell=True, universal_newlines=True) # Print the output print(output) You can now take advantage of the power of the command line from your code by leveraging the power of Python’s subprocess module.
Find elsewhere
🌐
CodeRivers
https://coderivers.org › blog › save-shell-output-to-variable-python
Saving Shell Output to a Variable in Python - CodeRivers
February 22, 2026 - The subprocess module provides a reliable and secure way to achieve this, while os.popen is a deprecated alternative. By following best practices in error handling, security, and performance optimization, you can write robust and efficient Python code that effectively uses shell commands and their output.
🌐
Quora
https://quora.com › How-do-I-pass-the-output-of-a-python-script-to-a-shell-variable
How to pass the output of a python script to a shell variable - Quora
Answer (1 of 5): In ‘bash’: VARIABLE=`python myscript.py` echo ${VARIABLE} Note that the quotes are “back-quotes” not regular single-quotes. In bash, that means “Run the thing between the back-quotes and replace everything between the quotes with whatever the program produced on the ...
🌐
Medium
https://pramodgupta12378.medium.com › mastering-python-storing-command-output-in-variables-419e030c0f43
Mastering Python: Storing Command Output in Variables | by Pramod Kumar Gupta | Medium
July 17, 2023 - By using Python’s powerful modules like os and subprocess, capturing the output of a command and storing it in a variable becomes a straightforward task. These techniques allow you to interact with the operating system efficiently and process the command output as needed for your projects or applications.
🌐
Python Forum
https://python-forum.io › thread-34738.html
Command output to Variable
August 26, 2021 - Good afternoon everyone. I am new to python and I am trying to get my self up to speed as soon as possible. I thought it would be easy since ive already worked with VBS and Powershell but this seems l
🌐
LinkedIn
https://linkedin.com › pulse › store-output-command-variable-using-python-harsh-joshi
To store the output of a command in a variable using Python
July 19, 2023 - python Copy code import subprocess ... output print(output) In the above example, the subprocess.check_output() function is used to run the command specified as a string....
🌐
nixCraft
https://cyberciti.biz › nixcraft › howto › python › python run external command and get output on screen or in variable
Python Run External Command And Get Output On Screen or In Variable - nixCraft
March 28, 2023 - For Python 3 compatibility change print to print(). For example: print("Foo bar", var1) In this example, run a standard Unix date command and display back the output on screen: #!/usr/bin/python ## get subprocess module import subprocess ## call date command ## p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True) ## Talk with date command i.e.
🌐
Medium
https://medium.com › @sharma.naman2022 › how-to-store-the-output-of-a-command-in-a-variable-using-python-b88e8082eefb
How to store the output of a command in a variable using Python. | by Naman Sharma | Medium
July 3, 2023 - By leveraging the subprocess.run() function and its options, you can execute system commands and store their output in variables for further processing. This capability allows you to seamlessly integrate command-line functionality within your ...
🌐
Reddit
https://reddit.com › r/bash › redirecting python output to bash variables
r/bash on Reddit: redirecting python output to bash variables
May 20, 2018 -

I have a python script that I want to run in a .sh file

the python script contains two print statements one of them is a number and another is a link

i would like to further use them in my bash as two separate variables.

!/bin/sh

outputString=$(python3 draft.py)
echo $outputString < -- this prints my number and a link that I want to assign to two variables to further use in my bash.

Any help is welcome

Thanks

EDIT:

I found the set command that seems to split it into variables and can call them with $1 and $2.

Thanks for the help guys.

outputString=$(python3 draft.py)

set $outputString

wget $2

gzip $1.gz -d

🌐
Linux Handbook
https://linuxhandbook.com › execute-shell-command-python
How to Execute Bash Shell Commands with Python
June 28, 2022 - Since the sysadmin tasks involve Linux commands all the time, running Linux commands from the Python script is a great help. In this tutorial, I’ll show you a couple of ways you can run shell commands and get its output in your Python program.
🌐
Stack Overflow
https://stackoverflow.com › questions › 73114098 › use-python-to-execute-shell-command-and-redirect-the-output-to-a-variable
Use python to execute shell command and redirect the output to a variable - Stack Overflow
import subprocess import json raw = subprocess.check_output(["curl", "http://ipwho.is/104.123.204.11"]) data = json.loads(raw) print(data) Although this works, it's rarely if ever a good idea to shell out to curl to retrieve a URL in Python.
🌐
W3docs
https://w3docs.com › home › code snippets › python › running shell command and capturing the output
Running shell command and capturing the output | W3docs
Running shell command and capturing the output in Python using the subprocess.check_output method · import subprocess output = subprocess.check_output(['ls','-l']) print(output.decode('utf-8')) ... In both cases, the output will be captured ...
🌐
Reddit
https://reddit.com › r/learnpython › how to run remote shell commands and save the output to a variable
r/learnpython on Reddit: How to run remote shell commands and save the output to a variable
June 22, 2022 -

I currently have this function which I can pass IPs into and it will display what I want. The issue is I am trying to take the output and perform a conditional against it. I feel like there is something simple I am missing here, but I can't seem to figure it out. Note: That print statement works, I just cannot get it into a variable (Always ends up empty it seems)

def linuxConnect(ip):
    client.connect(hostname=ip, username=linuxUname, password=linuxPwd)
    stdin, stdout, stderr = client.exec_command('nginx -v')
    print(stdout.read().decode())
    err = stderr.read().decode()
    if err:
        print(err)
    client.close()
    return