Let's understand our script in detail:
- The mytrap.sh script leverages functions and the trap call. Inside of the setup function, we set the function to be called by the trap command. Therefore, when Ctrl + C is called, the cleanup function is executed.
- Running the script will cause the script to run forever after printing out the PID of the script.
- Pressing regular keys such as Enter will not have an effect on the program.
- Pressing Ctrl+ C will echo cleanup on the console and the script will exit using the exit command.