Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Signal handling with trap in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Signal Handling 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 SIGINT is sent?
Consider this bash script that traps SIGINT (Ctrl+C) and prints a message before exiting. What will be printed if the user presses Ctrl+C while the script is running?
Bash Scripting
trap 'echo "Caught SIGINT, exiting..."; exit 1' SIGINT
while true; do
  echo "Running..."
  sleep 1
done
ARunning...\nRunning...\nCaught SIGINT, exiting...
BRunning...\nRunning...\nTerminated
CCaught SIGINT, exiting...
DRunning...\nRunning...\nRunning... (script never stops)
Attempts:
2 left
💡 Hint
Think about what the trap command does when SIGINT is received.
💻 Command Output
intermediate
2:00remaining
What happens if trap is not set for SIGTERM?
Given this script, what will happen if it receives a SIGTERM signal?
Bash Scripting
while true; do
  echo "Waiting..."
  sleep 2
done
AThe script terminates immediately without any message.
BThe script restarts automatically.
CThe script prints 'Terminated' and exits.
DThe script ignores SIGTERM and continues running.
Attempts:
2 left
💡 Hint
What is the default behavior of SIGTERM if not trapped?
📝 Syntax
advanced
2:00remaining
Identify the syntax error in this trap command
Which option contains the correct syntax to trap SIGUSR1 and run a function named handle_usr1?
Bash Scripting
function handle_usr1() {
  echo "SIGUSR1 received"
}

# trap command here
Atrap 'handle_usr1' SIGUSR1
Btrap handle_usr1 SIGUSR1
Ctrap "handle_usr1" SIGUSR1
Dtrap handle_usr1() SIGUSR1
Attempts:
2 left
💡 Hint
trap expects a command string or function name without parentheses.
🔧 Debug
advanced
2:00remaining
Why does this trap not work as expected?
This script is supposed to print 'Exiting...' when it receives SIGINT, but it doesn't. Why?
Bash Scripting
trap 'echo "Exiting..."' SIGINT

while true; do
  sleep 1
done
AThe trap command syntax is wrong; single quotes prevent execution.
BSIGINT cannot be trapped in an infinite loop.
CThe trap command is missing a command to exit, so the script continues running after printing.
DThe trap command is correct; the script ignores SIGINT because sleep is blocking.
Attempts:
2 left
💡 Hint
What happens after the trap command runs?
🚀 Application
expert
2:00remaining
How to safely clean up temporary files on script termination?
You want a bash script to create a temporary file and delete it when the script ends, whether normally or due to signals like SIGINT or SIGTERM. Which trap setup ensures cleanup in all cases?
Bash Scripting
tmpfile=$(mktemp)
echo "Temporary file created: $tmpfile"

# trap commands here

# Simulate work
sleep 30

# End of script
Atrap 'rm -f "$tmpfile"; exit' EXIT SIGINT SIGTERM
Btrap 'rm -f "$tmpfile"; exit' SIGINT SIGTERM
Ctrap 'rm -f "$tmpfile"' EXIT SIGINT SIGTERM
Dtrap 'rm -f "$tmpfile"' EXIT
Attempts:
2 left
💡 Hint
EXIT trap runs on script exit regardless of reason.

Practice

(1/5)
1. What does the trap command do in a bash script?
easy
A. It creates a loop that repeats commands indefinitely.
B. It catches signals like Ctrl+C and runs specified commands.
C. It defines a function to be used later in the script.
D. It pauses the script until a key is pressed.

Solution

  1. Step 1: Understand the purpose of trap

    The trap command is used to catch signals sent to the script, such as Ctrl+C (SIGINT).
  2. Step 2: What trap does with signals

    When a signal is caught, trap runs the commands specified by the user to handle that signal.
  3. Final Answer:

    It catches signals like Ctrl+C and runs specified commands. -> Option B
  4. Quick Check:

    trap catches signals = B [OK]
Hint: Trap runs commands on signals like Ctrl+C [OK]
Common Mistakes:
  • Thinking trap creates loops
  • Confusing trap with function definition
  • Assuming trap pauses script
2. Which of the following is the correct syntax to run echo "Bye" when the script receives a SIGINT signal?
easy
A. trap 'echo "Bye"' SIGINT
B. trap echo "Bye" SIGINT
C. trap (echo "Bye") SIGINT
D. trap {echo "Bye"} SIGINT

Solution

  1. Step 1: Check trap syntax for commands

    The commands to run must be quoted as a single string, usually with single quotes to preserve special characters.
  2. Step 2: Identify correct quoting style

    trap 'echo "Bye"' SIGINT uses single quotes around the entire command string, which is the correct syntax.
  3. Final Answer:

    trap 'echo "Bye"' SIGINT -> Option A
  4. Quick Check:

    trap 'commands' SIGNAL = A [OK]
Hint: Use single quotes around commands in trap [OK]
Common Mistakes:
  • Not quoting commands properly
  • Using parentheses or braces incorrectly
  • Omitting quotes causing syntax errors
3. What will be the output of this script when you press Ctrl+C during its execution?
#!/bin/bash
trap 'echo "Interrupted! Cleaning up..."; exit' SIGINT
while true; do
  echo "Running..."
  sleep 2
done
medium
A. The script ignores Ctrl+C and keeps running.
B. It crashes with an error message.
C. It prints "Running..." once and then exits.
D. It prints "Interrupted! Cleaning up..." and exits.

Solution

  1. Step 1: Understand trap on SIGINT

    The trap command catches the SIGINT signal (Ctrl+C) and runs the echo and exit commands.
  2. Step 2: Effect of trap during infinite loop

    When Ctrl+C is pressed, the script prints the message and exits immediately, stopping the loop.
  3. Final Answer:

    It prints "Interrupted! Cleaning up..." and exits. -> Option D
  4. Quick Check:

    trap on SIGINT prints message and exits = D [OK]
Hint: trap runs commands on Ctrl+C, then script exits [OK]
Common Mistakes:
  • Thinking script ignores Ctrl+C
  • Assuming loop stops without message
  • Expecting error instead of clean exit
4. Identify the error in this script snippet:
trap echo "Exiting" SIGTERM
sleep 10
medium
A. sleep command cannot be used with trap.
B. SIGTERM is not a valid signal name.
C. Missing quotes around the command to run in trap.
D. trap must be placed after sleep command.

Solution

  1. Step 1: Check trap command syntax

    The command to run on signal must be quoted as a single string to avoid syntax errors.
  2. Step 2: Identify missing quotes

    Here, echo "Exiting" is not quoted, so the shell misinterprets the command.
  3. Final Answer:

    Missing quotes around the command to run in trap. -> Option C
  4. Quick Check:

    trap commands need quotes = C [OK]
Hint: Always quote trap commands to avoid syntax errors [OK]
Common Mistakes:
  • Not quoting trap commands
  • Using invalid signal names
  • Placing trap after commands needing it
5. You want a bash script to delete a temporary file /tmp/mytempfile when it exits, whether normally or due to Ctrl+C. Which trap command correctly achieves this?
hard
A. trap 'rm -f /tmp/mytempfile' EXIT
B. trap 'rm -f /tmp/mytempfile' SIGINT
C. trap 'rm -f /tmp/mytempfile' SIGTERM
D. trap 'rm -f /tmp/mytempfile' SIGINT SIGTERM

Solution

  1. Step 1: Understand EXIT signal

    EXIT is a special trap signal that runs commands when the script ends for any reason, including normal exit or Ctrl+C.
  2. Step 2: Why EXIT is better than SIGINT or SIGTERM alone

    Using EXIT ensures cleanup runs on all script exits, not just specific signals.
  3. Final Answer:

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

    Use EXIT to clean up on all exits = A [OK]
Hint: Use trap on EXIT to clean up on any script exit [OK]
Common Mistakes:
  • Using only SIGINT or SIGTERM misses normal exit
  • Not quoting the command
  • Expecting multiple signals needed for all exits
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