Bird
Raised Fist0
Bash Scriptingscripting~20 mins

trap for cleanup on exit in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Trap Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output when the script exits?
Consider this Bash script that sets a trap to clean up a temporary file on exit. What will be printed when the script finishes?
Bash Scripting
#!/bin/bash

tmpfile=$(mktemp)
trap 'echo "Cleaning up $tmpfile"; rm -f "$tmpfile"' EXIT

echo "Temporary file created: $tmpfile"
exit 0
ATemporary file created: /tmp/tmp.abcd1234\nCleaning up /tmp/tmp.abcd1234
BTemporary file created: /tmp/tmp.abcd1234
CCleaning up /tmp/tmp.abcd1234
DNo output
Attempts:
2 left
💡 Hint
The trap command runs the cleanup command when the script exits.
💻 Command Output
intermediate
2:00remaining
What happens if the script is interrupted with Ctrl+C?
Given this Bash script, what output will appear if the user presses Ctrl+C during the sleep?
Bash Scripting
#!/bin/bash

tmpfile=$(mktemp)
trap 'echo "Cleaning up $tmpfile"; rm -f "$tmpfile"' EXIT

echo "Temporary file created: $tmpfile"
sleep 10
exit 0
ACleaning up /tmp/tmp.abcd1234
BTemporary file created: /tmp/tmp.abcd1234
CTemporary file created: /tmp/tmp.abcd1234\nCleaning up /tmp/tmp.abcd1234
DNo output
Attempts:
2 left
💡 Hint
The EXIT trap runs even if the script is interrupted.
📝 Syntax
advanced
2:00remaining
Which trap command correctly cleans up on script exit?
You want to run a cleanup command when the script exits. Which trap syntax is correct?
Atrap rm -f $tmpfile EXIT
Btrap {rm -f $tmpfile} EXIT
Ctrap (rm -f $tmpfile) EXIT
Dtrap 'rm -f $tmpfile' EXIT
Attempts:
2 left
💡 Hint
trap requires a quoted command string as the first argument.
🔧 Debug
advanced
2:00remaining
Why does this trap not run the cleanup command?
This script tries to clean up a file on exit but the cleanup command never runs. Why?
Bash Scripting
#!/bin/bash

tmpfile=$(mktemp)
trap 'rm -f $tmpfile' SIGINT

echo "Temporary file: $tmpfile"
sleep 5
exit 0
ABecause the trap is set only for SIGINT, not EXIT, so normal exit skips cleanup
BBecause $tmpfile is empty inside the trap command
CBecause sleep prevents trap from running
DBecause trap commands cannot remove files
Attempts:
2 left
💡 Hint
EXIT trap runs on any exit, SIGINT only on Ctrl+C.
🚀 Application
expert
3:00remaining
How to ensure cleanup runs even if script is killed by SIGTERM?
You want your Bash script to always remove a temporary file on exit, even if killed by SIGTERM. Which trap commands should you use?
Atrap 'rm -f "$tmpfile"' EXIT; trap 'exit' SIGTERM
Btrap 'rm -f "$tmpfile"' EXIT; trap 'rm -f "$tmpfile"' SIGTERM
Ctrap 'rm -f "$tmpfile"' EXIT SIGTERM
Dtrap 'rm -f "$tmpfile"' SIGTERM
Attempts:
2 left
💡 Hint
You can set multiple traps for different signals.

Practice

(1/5)
1. What is the main purpose of using trap in a bash script?
easy
A. To define functions inside the script
B. To create loops that repeat commands
C. To run specific commands when the script exits or receives signals
D. To declare variables with default values

Solution

  1. Step 1: Understand the role of trap

    The trap command is designed to catch signals or script exit events and run commands in response.
  2. Step 2: Compare options with trap's purpose

    Options B, C, and D describe other bash features unrelated to trap. Only To run specific commands when the script exits or receives signals correctly describes its purpose.
  3. Final Answer:

    To run specific commands when the script exits or receives signals -> Option C
  4. Quick Check:

    trap runs commands on exit/signals = A [OK]
Hint: Trap runs cleanup commands on exit or signals [OK]
Common Mistakes:
  • Confusing trap with loops or functions
  • Thinking trap declares variables
  • Assuming trap runs commands continuously
2. Which of the following is the correct syntax to run cleanup function on script exit using trap?
easy
A. trap EXIT cleanup
B. trap 'cleanup' on EXIT
C. trap cleanup on EXIT
D. trap 'cleanup' EXIT

Solution

  1. Step 1: Recall trap syntax for commands

    The command(s) to run must be quoted as a string, followed by the signal or event name.
  2. Step 2: Check each option's syntax

    trap 'cleanup' EXIT correctly quotes the command and uses the signal EXIT. trap EXIT cleanup places the signal before the command (wrong order). Options C and D use incorrect keywords like 'on'.
  3. Final Answer:

    trap 'cleanup' EXIT -> Option D
  4. Quick Check:

    trap 'command' SIGNAL = B [OK]
Hint: Always quote commands in trap before the signal [OK]
Common Mistakes:
  • Omitting quotes around commands
  • Using 'on' keyword incorrectly
  • Placing signal before command
3. What will be the output of this script?
#!/bin/bash
cleanup() {
  echo "Cleaning up temp files"
}
trap 'cleanup' EXIT

echo "Script is running"
medium
A. Script is running
B. Script is running\nCleaning up temp files
C. No output
D. Cleaning up temp files

Solution

  1. Step 1: Understand script flow

    The script prints "Script is running" first, then exits.
  2. Step 2: Effect of trap on EXIT

    On exit, the cleanup function runs, printing "Cleaning up temp files" after the first echo.
  3. Final Answer:

    Script is running\nCleaning up temp files -> Option B
  4. Quick Check:

    trap runs cleanup on exit, so both lines print [OK]
Hint: trap runs cleanup after script finishes [OK]
Common Mistakes:
  • Thinking trap runs before script output
  • Expecting only cleanup message
  • Ignoring trap on EXIT
4. Identify the error in this script snippet that uses trap for cleanup:
#!/bin/bash
trap cleanup EXIT

cleanup() {
  echo "Cleaning up"
}
medium
A. No error, script is correct
B. EXIT signal is invalid
C. trap command missing quotes around cleanup
D. cleanup function is defined after trap command

Solution

  1. Step 1: Check trap command syntax

    For simple function names like cleanup, quotes are not required around the command in trap.
  2. Step 2: Analyze script order and signals

    Defining cleanup after trap is allowed because bash processes function definitions before executing traps. EXIT is valid. No syntax errors present.
  3. Final Answer:

    No error, script is correct -> Option A
  4. Quick Check:

    unquoted function name OK, post-definition OK [OK]
Hint: trap accepts unquoted simple function names [OK]
Common Mistakes:
  • Thinking functions must be defined before trap
  • Believing EXIT is an invalid signal
  • Assuming quotes are required around simple commands
5. You want to ensure a temporary file /tmp/mytempfile is deleted when your bash script exits, even if interrupted. Which trap command correctly achieves this?
hard
A. trap 'rm -f /tmp/mytempfile' EXIT INT TERM
B. trap rm -f /tmp/mytempfile EXIT INT TERM
C. trap 'rm -f /tmp/mytempfile' ONEXIT
D. trap 'rm -f /tmp/mytempfile' EXIT SIGNAL

Solution

  1. Step 1: Understand trap for multiple signals

    To catch script exit and interruptions, use signals EXIT, INT, and TERM together.
  2. Step 2: Check syntax correctness

    Commands must be quoted. trap 'rm -f /tmp/mytempfile' EXIT INT TERM quotes the command and lists valid signals. Options B misses quotes, C uses invalid signal ONEXIT, D uses invalid SIGNAL keyword.
  3. Final Answer:

    trap 'rm -f /tmp/mytempfile' EXIT INT TERM -> Option A
  4. Quick Check:

    trap 'command' SIGNALS with quotes = A [OK]
Hint: Quote commands and list all signals to catch [OK]
Common Mistakes:
  • Missing quotes around commands
  • Using invalid signal names
  • Not including INT and TERM for interrupts
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard