Reputation: 2072
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
0
I would like the bash function to print the exit code of the python script.
Upvotes: 3
Views: 85
Reputation: 1060
Because python will not call main
by default. You have to call it explicitly.
You want:
import sys
def main():
sys.exit(1)
if __name__ == "__main__":
main()
Upvotes: 4
Reputation: 223062
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.
Upvotes: 5