sys.exit(myString) doesn't mean "return this string". If you pass a string to sys.exit, sys.exit will consider that string to be an error message, and it will write that string to stderr. The closest concept to a return value for an entire program is its exit status, which must be an integer.

If you want to capture output written to stderr, you can do something like

python yourscript 2> return_file

You could do something like that in your bash script

output=$((your command here) 2> &1)

This is not guaranteed to capture only the value passed to sys.exit, though. Anything else written to stderr will also be captured, which might include logging output or stack traces.

example:

test.py

print "something"
exit('ohoh') 

t.sh

va=$(python test.py 2>&1)                                                                                                                    
mkdir $va

bash t.sh

edit

Not sure why but in that case, I would write a main script and two other scripts... Mixing python and bash is pointless unless you really need to.

import script1
import script2

if __name__ == '__main__':
    filename = script1.run(sys.args)
    script2.run(filename)
Answer from Loïc Faure-Lacroix on Stack Overflow
Discussions

linux - return value from python script to shell script - Stack Overflow
I am adding a call from the shell to a Python script. i need to pass arguments from the shell to Python. i need to print the value returned from Python in the shell script. ... #!/bin/bash # script for testing clear echo "............script started............" sleep 1 python python/python... More on stackoverflow.com
🌐 stackoverflow.com
redirecting python ouput to bash variables
This isn't a python question, it's a bash question. Use $(command). it's better than `command`. Other than that ask in a bash forum. More on reddit.com
🌐 r/learnpython
6
7
May 20, 2018
linux - How to return a value from a shell script in a python script - Stack Overflow
What if this "a.sh" script is an interactive script which prompts user few question. In that case how can we capture those values in python script...? ... Subprocess.call returns return code of that script. More on stackoverflow.com
🌐 stackoverflow.com
May 23, 2017
Return value to Python script from Bash script - Stack Overflow
Except of the status code (an 8 ... can't "return" anything to the parent process. You have to invent some other solution: For instance, writing the value to stdout and catching the stdout inside Python (disadvantage: if your script writes other stuff to stdout, for instance for debugging, this will also be caught) or having the child process write the string to a file, and read the file from Python... More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
6

Within sh which is actually dash on Ubuntu the builtin command return can returns only numerical values - exit statuses, which have a meaning in a context of a function or sourced script. Source man sh:

The syntax of the return command is

return [exitstatus]

Everything other with your shell script looks correct. I think you need to use echo $COLOR instead return and suppress other echo-es.

In case you need to return more data to the main script you can output everything as one line and divide the separate fields by some character that will play role of a delimiter in the main script on which base you can convert the string into an array. For example (where , is our delimiter and -n will suppers the newline character within echo):

echo -n "$COLOR","$exitstatus"

The other information that is provided by the script and is not required by the main script could be redirected to some log file:

$ cat whiptail.sh
#!/bin/sh
log_file='/tmp/my.log'

COLOR=$(whiptail --inputbox "What is your favorite Color?" 8 78 Blue --title "Example Dialog" 3>&1 1>&2 2>&3)
exitstatus=exitstatus = 0 ]; then
    echo "User selected Ok and entered $COLOR" > "$log_file"
    echo -n "$COLOR","$exitstatus"
else
    echo "User selected Cancel."  >> "$log_file"
    echo -n "CANCEL","$exitstatus"
fi

Unfortunately I don't have much experience with Python, but here is a sample .py script that can handle the output of the above .sh script (reference):

$ cat main-script.py
#!/usr/bin/python
import subprocess

p = subprocess.Popen(['./whiptail.sh'], stdout=subprocess.PIPE)
p = p.communicate()[0]
p = p.split(",")
print "Color:    " + p[0]
print "ExitCode: " + p[1]
2 of 2
0

I had a similar problem where I needed the return value from a shell command in my Python script.

The subprocess method check_output() helped me get the shell return code as a byte string in the Python file.

Here is the code:

return_value =subprocess.check_output("sudo raspi-config nonint get_spi", stderr=subprocess.STDOUT, shell = True)
print(return_value)


<<<b'1\n'
🌐
Quora
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 standard o...
🌐
Quora
quora.com › How-do-I-get-return-values-from-Python-function-in-bash-array-by-calling-Python-script-from-bash
How to get return values from Python function in bash array by calling Python script from bash - Quora
Answer: While there are ways to do this, I would suggest avoiding using an array in Bash if possible. I would suggest streaming over data from a pipe instead. [code]./my_script.py | while IFS= read -r line; do something_with "$line" done [/code]In this case, each item from the list would hav...
🌐
A Passionate Techie
apassionatechie.wordpress.com › 2018 › 01 › 16 › how-to-access-python-return-value-from-bash-script
How to access python return value from bash script – A Passionate Techie
January 16, 2018 - How can I store in script_output the return value (execution ok)? If I direct execution ok to stdout, the script_output will capture all the stdout (so the 2 print statement). ... Add a proper exit code from your script using the sys.exit() module. Usually commands return 0 on successful completion of a script. import sys def main(): print ("exec main..") sys.exit(0) and capture it in shell script with a simple conditional.
Find elsewhere
🌐
Medium
medium.com › @nawazmohtashim › call-python-script-from-bash-909afd5248a3
Call Python Script from Bash. In the world of software development… | by Mohd Mohtashim Nawaz | Medium
December 21, 2023 - #!/bin/bash # Get user input for arguments echo "Enter argument 1:" read arg1 echo "Enter argument 2:" read arg2 echo "Enter argument 3:" read arg3 # Call the Python script with user-provided arguments python3 myscript.py "$arg1" "$arg2" "$arg3" This modification allows the user to input values for the arguments interactively.
🌐
CSDN
devpress.csdn.net › python › 62fd0ddac677032930802a0d.html
store return value of a Python script in a bash script_python_Mangs-Python
August 17, 2022 - EDIT: I need the string because that tells me where a file created by the Python script is located. I want to do something like: fileLocation=`python myPythonScript1 arg1 arg2 arg1` python myPythonScript2 $fileLocation · sys.exit(myString) doesn't mean "return this string". If you pass a string to sys.exit, sys.exit will consider that string to be an error message, and it will write that string to stderr. The closest concept to a return value for an entire program is its exit status, which must be an integer.
🌐
Reddit
reddit.com › r/learnpython › redirecting python ouput to bash variables
r/learnpython on Reddit: redirecting python ouput 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

🌐
Stack Overflow
stackoverflow.com › questions › 73291127 › return-value-to-python-script-from-bash-script
Return value to Python script from Bash script - Stack Overflow
import subprocess #my Python script #now call bash script using check_call subprocess.check_call(['path_to_bash/bash.sh', arg1, arg2, arg3]) #next Python line · Suppose the bash.sh is as following: #!/bin/bash v1=$1 v2=$2 v3=$3 echo $v1+$v2+$v3 · I want to return some value (in this case, the sum of all three variables) from the running of bash.sh script back to Python.
🌐
CopyProgramming
copyprogramming.com › howto › return-value-from-python-script-to-shell-script
Returning Values from Python Scripts to Shell Scripts: Complete Guide for 2026
December 17, 2025 - #!/bin/bash # Read multiple lines from Python output while IFS= read -r filename; do echo "Processing: $filename" done < <(python3 my_script.py) When your shell script needs to call Python and process its return value, Python's subprocess module provides powerful tools for managing external processes.
🌐
Python
python-list.python.narkive.com › Guc5JaIR › return-a-value-to-shell-script
return a value to shell script
[jeff at marvin ~]$ python -c 'import sys; sys.exit(0)' ; echo $? 0 [jeff at marvin ~]$ python -c 'import sys; sys.exit(1)' ; echo $? 1 [jeff at marvin ~]$ python -c 'import sys; sys.exit(255)' ; echo $? 255 [jeff at marvin ~]$ python -c 'import sys; sys.exit(256)' ; echo $? 0 Depending on what you're doing, printing from your Python program and capturing standard out might be a better approach. HTH, Jeff ... Hi, I am executing a python script in a shell script. The python script actually returns a value.
🌐
Medium
medium.com › @nawazmohtashim › print-ouput-from-python-script-in-bash-d5d7b37ba0c1
Print Ouput From Python Script in Bash | by Mohd Mohtashim Nawaz | Medium
February 2, 2024 - One of the simplest ways to capture the output of a Python script in Bash is through command substitution. This involves using the $() syntax or backticks to execute a command within a subshell and capture its output.