Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator is writing a Bash script to back up a directory. The script must continue running even if a particular command fails, but the administrator wants to log the error and take corrective action. Which of the following should be used to handle the error condition?

⚠ Common exam trap

The trap here is assuming that `set -e` is always the best way to handle errors, but it forces an exit and prevents custom recovery logic.

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

✓

Check the exit status of the command using `$?` and conditionally execute recovery steps.

The requirement is to continue after a failure while logging and taking corrective action. Checking `$?` immediately after a command allows the script to branch based on success or failure, enabling custom error handling without terminating the script. This approach is flexible and standard for scenarios where fail-fast is not desired. Other methods either exit prematurely or fail to provide error handling.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Use `trap` to catch the ERR signal and exit the script.

    Why it's wrong here

    `trap` can catch signals, but using it to exit on ERR would terminate the script, which contradicts the need to continue. While `trap` can run a handler on ERR, the handler would need to avoid exiting, but the option explicitly says 'and exit the script', which is incorrect. The scenario requires continuation, not termination.

  • ✗

    Run the command in a subshell and ignore its exit status.

    Why it's wrong here

    Running in a subshell does not inherently ignore the exit status; the subshell's exit status becomes the command's status. Ignoring the status means no logging or corrective action occurs, which fails the requirement. The administrator must actively check and respond to errors, not ignore them.

  • ✓

    Check the exit status of the command using `$?` and conditionally execute recovery steps.

    Why this is correct

    After running a command, `$?` holds its exit status. By checking `$?` immediately, the script can detect failure, log the error, and run corrective actions without terminating. This allows the script to continue and meets the requirement of logging and handling the error. It is the standard way to implement custom error handling in Bash.

  • ✗

    Use `set -e` at the beginning of the script.

    Why it's wrong here

    `set -e` causes the script to exit immediately if any command returns a non-zero status, which would prevent the script from continuing and handling the error gracefully. It is useful for fail-fast behavior, but here the requirement is to log and continue, so `set -e` is counterproductive. The administrator needs explicit error handling, not automatic exit.

About these practice questions

One of 781 original XK0-006 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 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.