Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does bash echo print 0 for a Python function that exits 1

Tags:

python

bash

With a Python function in testreturn.py defined as:

import sys
def main():
    sys.exit(1)

When called from a bash function in scrip.sh defined as:

#!/bin/bash
function test()
{  
   python testreturn.py
   echo $?
}

Why does the following command:

test

Print

0

I would like the bash function to print the exit code of the python script.

like image 852
Dan Cook Avatar asked Nov 25 '25 13:11

Dan Cook


1 Answers

Because you're defining, but never calling the main() function. The code finishes as normal (by reaching the end of the file) and return code is 0 in this case.

like image 174
nosklo Avatar answered Nov 28 '25 01:11

nosklo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!