Run a command right before a script exits due to failure Run a command right before a script exits due to failure unix unix

Run a command right before a script exits due to failure


In the script you posted, the fact that the shell exits is unrelated to any error. The shell would exit, because the last argument hast been executed. Take for instance the script

#!/bin/zshpython -c 'a'echo This is the End

The final echo will always be exeuted, independent of the python command. To cause the script to exit, when python returns a non-zero exit code, you would write something like

#!/bin/zshpython -c 'a' || exit $?echo Successful

If you want to exit a script, whenever the first one of the commands produces a non-zeror exit status, AND at the same time want to print a message, you can use the TRAPZERR callback:

#!/bin/zshTRAPZERR() {  echo You have an unhandled non-zero exit code in your otherwise fabulous script  exit $?}python -c 'a'echo Only Exit Code 0 encountered