Introduction
Use trap to run cleanup code automatically when your script ends or is interrupted. This helps keep things tidy and safe.
You want to delete temporary files when your script finishes or stops.
You need to close network connections or release resources before exiting.
You want to show a goodbye message or log info when the script ends.
You want to handle user pressing Ctrl+C to stop the script safely.