Courseiva
Create simple shell scriptshardMultiple ChoiceObjective-mapped

EX200 Create simple shell scripts Practice Question

You maintain a script that performs a long-running task and must clean up temporary files if the script is interrupted. The script uses:

#!/bin/bash tempfile=$(mktemp) trap "rm -f $tempfile" EXIT

# long task

sleep 100

You notice that if the script receives SIGINT (Ctrl+C), the temporary file is not removed. Investigation shows that the trap on EXIT is not executed on SIGINT. Which modification should be made?

⚠ Common exam trap

Red Hat often tests the misconception that the EXIT trap handles all termination scenarios, including signals, when in fact it only runs on normal exit paths, not on unhandled signals like SIGINT.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

Add a trap for INT: `trap "rm -f $tempfile; exit" INT`.

The EXIT trap is only triggered on normal script termination (e.g., reaching the end or an explicit `exit`), not on signals like SIGINT. By adding a separate trap for INT that explicitly removes the temp file and calls `exit`, the cleanup runs even when the user presses Ctrl+C, ensuring the temporary file is deleted.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • Move the trap inside a subshell: (trap ... EXIT; long task).

    Why it's wrong here

    Moving the trap into a subshell confines it to that child shell's lifetime. When the parent script receives SIGINT, the kernel terminates the parent process directly, and because the EXIT trap was never installed in the parent, no cleanup is performed there. Even if the subshell's trap fires, it only removes the file in the subshell's context, but the parent still dies without executing the same trap. Thus the symptom persists.

  • Change `trap ... EXIT` to `trap ... 0`.

    Why it's wrong here

    In bash, the signal specification 0 is literally synonymous with EXIT: both are special pseudo-signals that trigger only when the shell is about to terminate normally. Changing 'EXIT' to '0' therefore does not install any handler for SIGINT. When a user presses Ctrl-C, the shell's default SIGINT disposition kills the whole script immediately, and the EXIT/0 trap is still not invoked. This option merely changes notation without altering signal behavior.

  • Add `set -e` at the beginning of the script.

    Why it's wrong here

    Setting -e makes the shell abort on any command that returns a non-zero status, but it has nothing to do with signal dispatch. SIGINT is not an ordinary command failure; it is an asynchronous signal whose default action is process termination. A trap on EXIT is not guaranteed to run when the script is killed by SIGINT, and set -e does not intercept or convert that signal into a cleanup event. So the temp file remains after Ctrl-C.

  • Add a trap for INT: `trap "rm -f $tempfile; exit" INT`.

    Why this is correct

    This is the only option that explicitly handles the signal that is actually interrupting the script. The trap directive installs a handler for SIGINT, so when Ctrl-C is sent, the shell runs "rm -f $tempfile; exit" instead of simply dying. Because the trap is installed at the top level before the long task begins, it remains in effect throughout the task, and the explicit exit prevents the shell from continuing after the cleanup runs. This guarantees the temporary file is removed even on user interruption.

About these practice questions

One of 127 original EX200 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This EX200 practice question is part of Courseiva's free Red Hat certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the EX200 exam.