trap "$instructions" SIGSEGV traps segmentation faults in the shell itself.
If you run your script under
set -e, you can put a trap on
0). It will be executed when your script terminates (whether due to a command returning a nonzero status, or by explicitly calling
exit or by falling off the end of the script). To test for a segmentation fault, check
$? on entry to the trap. (Note that
$? could be 139 because the program returned normally with the status 139; this is avoidable if you do your processing in the shell.)
trap 'case $? in
139) echo "segfault occurred";;
In bash or ksh or zsh, you don't need to use
set -e to execute a trap after each command that returns a nonzero status, you can put a trap on
ERR instead. As before, you need to check
$? on entry to the trap, and 139 can (but rarely does) mean that the program returned this status.