Bash Scripting - Error HandlingYou want to ensure temporary files are deleted when your script exits, even if interrupted. Which trap setup is best?Atrap 'rm -f /tmp/mytempfile' EXIT SIGINT SIGTERMBtrap 'rm -f /tmp/mytempfile' EXITCtrap 'rm -f /tmp/mytempfile' SIGKILLDtrap 'rm -f /tmp/mytempfile' SIGSTOPCheck Answer
Step-by-Step SolutionSolution:Step 1: Identify signals to trap for cleanupEXIT runs on normal exit, SIGINT and SIGTERM cover interrupts and termination.Step 2: Choose trap covering all exit casestrap 'rm -f /tmp/mytempfile' EXIT SIGINT SIGTERM traps EXIT, SIGINT, and SIGTERM to delete temp file in all cases.Final Answer:trap 'rm -f /tmp/mytempfile' EXIT SIGINT SIGTERM -> Option AQuick Check:Trap multiple signals to ensure cleanup on exit and interrupts [OK]Quick Trick: Trap EXIT, SIGINT, SIGTERM for reliable cleanup [OK]Common Mistakes:MISTAKESTrying to trap SIGKILL or SIGSTOP which cannot be trappedOnly trapping EXIT misses interruptsNot quoting commands in trap
Master "Error Handling" in Bash Scripting9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Bash Scripting Quizzes Arrays - Accessing array elements - Quiz 6medium Arrays - Accessing array elements - Quiz 14medium Error Handling - Error logging patterns - Quiz 7medium Functions - Why functions organize reusable code - Quiz 10hard String Operations - String suffix removal (${var%pattern}) - Quiz 12easy Text Processing in Scripts - Here documents (<<EOF) - Quiz 2easy Text Processing in Scripts - Why scripts often process text - Quiz 9hard Text Processing in Scripts - Why scripts often process text - Quiz 13medium Text Processing in Scripts - Why scripts often process text - Quiz 10hard Text Processing in Scripts - sort and uniq in pipelines - Quiz 4medium