Introduction
Signal handling lets your script respond to events like pressing Ctrl+C. Using trap helps your script clean up or stop safely.
trap 'commands' SIGNALStrap 'echo "Script interrupted!"; exit' SIGINTtrap 'rm -f /tmp/tempfile' EXITtrap '' SIGTERM#!/bin/bash trap 'echo "You pressed Ctrl+C! Cleaning up..."; exit' SIGINT echo "Script is running. Press Ctrl+C to stop." while true; do sleep 1 done
trap command do in a bash script?echo "Bye" when the script receives a SIGINT signal?#!/bin/bash trap 'echo "Interrupted! Cleaning up..."; exit' SIGINT while true; do echo "Running..." sleep 2 done
trap echo "Exiting" SIGTERM sleep 10
/tmp/mytempfile when it exits, whether normally or due to Ctrl+C. Which trap command correctly achieves this?Pass the test and this pattern moves unknown