Bash Trap Control C

Do you want to catch control-c keyboard interrupts in your Bash program? Use the Bash builtin trap command to catch system signals. The following runs control_c() when a user interrupts the main() section with a Control-C (SIGINT).

# example cleanup function
  rm -f /tmp/tempfile
  return $?
# run if user hits control-c
  echo -en "\n*** Ouch! Exiting ***\n"
  exit $?
# trap keyboard interrupt (control-c)
trap control_c SIGINT
# main() loop
while true; do read x; done