XK0-006 Automation, Orchestration, and Scripting Practice Question
An administrator is writing a Bash script that must run a cleanup function when the script exits, whether it finishes normally or is interrupted. Which construct guarantees the function runs in both cases?
⚠ Common exam trap
Many exam-takers confuse set -e, which controls when the shell aborts, with trap on EXIT, which actually runs cleanup at termination.
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 facility lets a script associate a handler with a signal or with the EXIT pseudo-signal, which the shell raises on every termination path. Using trap 'cleanup' EXIT ensures the teardown function executes after normal completion and after interrupts, unlike aliases, backgrounding, or plain sequential execution, which have no exit semantics.
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 &
Why it's wrong here
Running the function in the background merely starts it asynchronously; it does not schedule execution at exit. The script could terminate before the background job finishes, and if the function relies on variables or files that the parent removes, it may fail. This does not address the interrupt case at all.
- ✗
set -e; cleanup
Why it's wrong here
The set -e option makes the shell exit on an unchecked failing command, but calling cleanup on the next line runs it immediately, not at exit. It would execute even if later commands fail before reaching the teardown logic, and it would not run if the script is killed by a signal, so the guarantee is absent.
- ✗
alias cleanup='rm -rf /tmp/work'
Why it's wrong here
An alias only substitutes text when the command name is typed interactively or expanded in a script where alias expansion is enabled, and it never triggers automatically. Nothing calls cleanup at exit, so temporary files would be left behind. Aliases are also non-inherited by non-interactive shells by default.
- ✓
trap 'cleanup' EXIT
Why this is correct
The EXIT pseudo-signal fires whenever the shell terminates, including normal completion, explicit exit, and receipt of a termination signal that the shell handles. Registering cleanup with trap therefore guarantees the function runs on both paths, making it the standard idiom for resource teardown in scripts.
Go deeper
Related to this question
Learn chapter
Advanced Shell Scripting and Automation
Key term
Shell
A shell is a computer program that provides a user interface to access an operating system's services, typically by accepting text commands.
Key term
Bash script
A Bash script is a text file containing a sequence of commands for the Unix shell Bash, allowing users to automate repetitive tasks and streamline system administration on Linux and macOS.
About these practice questions
This XK0-006 question is part of Courseiva's 781-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →
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 CompTIA exam blueprint
This XK0-006 practice question is part of Courseiva's free CompTIA 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 XK0-006 exam.