Working with exit codes between Python & Shell Scripts

Anup Kumar Ray
3 min readOct 17, 2021

--

Photo by chris robert on Unsplash

What is an exit code?

An exit code is received when a command or a script is executed. An exit code is a system response reporting success, failure or any other condition which provides a clue about what caused the (unexpected) result from the command/script.

  • Exit codes don’t reveal itself unless someones asks for it.
  • Exit codes are useful for code debugging.
  • Exit codes are useful in different system integrations

Synonyms: Exit Status, Return Code, Exit Status Code

Wikipedia definition of Exit Code

Exit codes between Python & Shell Scripts

Often times, we deal with systems which involves multiple different programming languages where one program(child) is called from within another(parent). Depending on the (exit)status of the child, rest of the parent program flow is executed. In this case, handling the exit codes is of paramount importance.

In this article, we will explore on the ways of working with exit codes between python and shell scripts. So let’s dive right in :)

An exit code in shell script is captured by executing $?

[Case: 1] Standard Exit Codes

Consider the following Python (StandardExitCode.py) & Shell Script (StandardExitCode.sh):

On executing the shell script (which calls the python code), we get standard exit code as shown below:

[Case: 2] Custom Exit Codes

Consider the following Python (CustomExitCode.py) & Shell Script (CustomExitCode.sh):

Executing CustomExitCode.sh will result in capturing the custom code 9 from the Python program

Note: If you pass anything other than an integer to sys.exit() call, the passed value will be printed and system exit status will be 1 . Refer the below example:

[Case: 3a] Python Exceptions & Exit Codes (with proper exception handling in Python)

Consider the following Python (Exceptions.py) & Shell Script (Exceptions.sh):

Since exception is properly handled in python code, it will exit with a success return code 0 when Exceptions.sh is executed.

[Case: 3b] Python Exceptions & Exit Codes (raising exception in Python)

Executing the shell code will now result in printing the exception on terminal and exit code will be of failure i.e. 1

Conclusion

In this article, we saw some very simple cases of handling exit codes between Python & Shell Scrips. Crux of the matter is:

  1. Standard exit codes are received when python program executes. Successful execution returns 0 and unsuccessful execution returns 1
  2. Custom exit codes can be passed using sys.exit() call in python. Useful when attaching error descriptions to exit codes. Anything other than Integer argument to the call is printed and system exits with error code 1
  3. Exceptions when handled properly in python code, standard exit code rules apply.
  4. When exceptions are raised in python, exit code 1 is passed and exception is printed on the terminal.

Please let me know if this was worth reading. Share your feedback and suggestions via comments. Happy Programming!!!

--

--