Running bash
4.3 on Kubuntu Trusty 64 bit. Please see the following two files:
trapping-int.sh
#! /bin/bash
trap "echo Exiting" INT
cat </dev/urandom >/dev/null
echo Hello
trapping-exit.sh
#! /bin/bash
trap "echo Exiting" EXIT
cat </dev/urandom >/dev/null
echo Hello
Now upon hitting ^C, trapping SIGINT prints both Exiting
and Hello
whereas trapping EXIT prints Exiting
only but with an extra newline:
$ ./trapping-int
^CExiting
Hello
$ ./trapping-exit
^CExiting
$
I would like to know the reason for the differing behaviour. Also, can I safely assume that EXIT is always called even if INT is given by ^C?
Commenting out the last line in both scripts causes trapping SIGINT to no longer print the Hello but trapping EXIT still prints the extra newline. I'd like to know the reason here too.
Thanks!