trap is a command which is rarely used for most linux shell developers. But sometimes it is very useful, especially when u have some background jobs running and would like to cleanup such jobs if the scripts exits abnormally.

This page has an excellent explanation for trap command.

Besides it gives the difference between what “kill –SIGINT pid” does and CTRL+C, it is

  • the first applys to a process pid
  • the second applys to the forground job, all processes which belong to that job will receive SIGINT

Another tip is of using “& wait” make bash script asynchrous, so that  trap Command List can be executed immediately instead of only being run after the script’s  foreground process is complete.