bashsignalsexit-codebash-trap

How to trap exit code in Bash script


There're many exit points in my bash code. I need to do some clean up work on exit, so I used trap to add a callback for exit like this:

trap "mycleanup" EXIT

The problem is there're different exit codes, I need to do corresponding cleanup works. Can I get exit code in mycleanup?


Solution

  • I think you can use $? to get the exit code.