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

found at:


Popular posts from this blog

Securing the Pi-hole with fail2ban to prevent DNS Amplification attacks

How to clean DB from old logs in Magento 1.x

Python pxssh failed on login. could not set shell prompt