Reputation: 22512
Is it possible to get the exit code of the previous command that as run on the shell in python?
I want to write a script that will return the same error code that is currently set (assuming the python command is successful). It would be the equivalent of doing something like this in bash:
#!/bin/bash
err=$?
# Do something...
exit $err
I would like to avoid passing it in as an argument, if possible. For example:
python script.py $?
Upvotes: 2
Views: 3425
Reputation: 307
I'd say that your solution (wrapping both the "previous command" and the Python script in a shell script that keeps track of the return codes) is the way to go:
#!/bin/bash
first_command
ERR=$?
python second_script.py
exit $ERR
The Python script will be more reusable as it won't have to rely on any assumption about its environment.
Any reason why this wrap solution doesn't fit your case?
Upvotes: 2
Reputation: 148890
IMHO it is not possible. A command started by bash has no access to the shell variables nor to the shell itself. It can only knows :
But I do not know any bash API to get more.
Upvotes: 2