Courseiva

LPIC-1 Shells, Scripting and Data Management Practice Question

A shell script contains a function named `cleanup` that must run automatically when the script exits, whether it finishes normally or is terminated by a signal. Which construct should be used to register this function?

⚠ Common exam trap

The trap here is assuming that trapping INT and TERM also covers normal exit; only the EXIT pseudo-signal fires on ordinary script completion.

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

✓

trap cleanup EXIT

The `trap ... EXIT` form registers a handler for the shell's exit event, which fires on normal termination as well as on exits caused by signals the shell processes. That covers both cases in the scenario, whereas trapping only specific signals would miss normal completion and merely calling the function would not defer it.

Answer analysis

Option-by-option breakdown

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

  • ✗

    cleanup && exit

    Why it's wrong here

    This line simply runs `cleanup` and, if it succeeds, exits the script at that point. It does not arrange for cleanup to run later on termination, and it changes the script's control flow immediately. It is not a registration mechanism for an exit handler.

  • ✓

    trap cleanup EXIT

    Why this is correct

    The `trap` builtin with the EXIT pseudo-signal arranges for the named command or function to run when the shell exits, including normal completion and exits triggered by signals that the shell handles. Registering `cleanup` this way ensures the function executes in both scenarios described, making it the correct construct.

  • ✗

    trap cleanup INT TERM

    Why it's wrong here

    This registers the handler for the SIGINT and SIGTERM signals but not for normal script termination. If the script reaches its end without receiving either signal, `cleanup` never runs. The requirement explicitly includes normal completion, so signal-specific traps alone are insufficient.

  • ✗

    at_exit cleanup

    Why it's wrong here

    Bash has no `at_exit` builtin; that concept exists in other languages such as Perl and Ruby. Using this line in a script produces a command-not-found error, and no cleanup handler is registered. It is a plausible-sounding but nonexistent construct for the Bash shell.

About these practice questions

One of 402 original LPIC-1 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 and reviewed by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

Last reviewed September 2026 · checked against the official LPI exam blueprint

This LPIC-1 practice question is part of Courseiva's free LPI 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 LPIC-1 exam.