Alex Rivera | Logout

How to trap exit code in Bash script

Asked 2011-03-15T13:15:50.063
84

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?

Edit
Report

1 Answer

64

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

answered 2011-03-15T13:32:56.577

Your Answer