Trap Interrupts in Shell Scripts

Today I met some shell scirpts like the following:

1
trap "rm -f $file; exit" INT TERM EXIT

More code here:

1
2
3
4
5
6
7
8
9
cleanup() {
    echo "Cleaning stuff up..."
    exit
}

trap cleanup INT TERM
echo ' --- press ENTER to close --- '
read var
cleanup

change “trap cleanup INT TERM” above to “trap cleanup INT TERM EXIT” and then execute the script. Enter ‘exit’ and see what differences will happen.

The following are references:

1
2
http://unix.stackexchange.com/questions/57940/trap-int-term-exit-really-necessary
http://kb.mit.edu/confluence/pages/viewpage.action?pageId=3907156