Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator wants to ensure a Bash script exits immediately if any command fails. Which directive should be included at the beginning of the script?

⚠ Common exam trap

XK0-006 often tests the difference between `set -e`, `set -u`, `set -x`, and `set -o pipefail` — candidates confuse the fail-fast directive with pipefail or with debugging flags, especially because all four are commonly written together as `set -euxo pipefail`.

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

✓

set -e

The `set -e` directive (equivalent to `set -o errexit`) causes the shell to exit immediately when any command returns a non-zero exit status, unless that command is part of a condition (if/while/until) or followed by `||`. This is the standard way to make a Bash script fail fast on the first error. Placing it near the top of the script ensures subsequent commands are not executed after a failure.

Answer analysis

Option-by-option breakdown

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

  • ✗

    set -o pipefail

    Why it's wrong here

    pipefail only changes a pipeline's exit status to reflect a failing component; it does not itself terminate the script on a failing command. The required directive is set -e. pipefail is tempting because it strengthens error detection within pipelines, and it would be correct alongside set -e when pipeline failures must also be caught.

  • ✗

    set -u

    Why it's wrong here

    set -u makes the shell treat expansion of an unset variable as an error; it does not exit on a command returning non-zero. The scenario requires set -e. set -u is tempting because it also causes early termination, but only for unset-variable references, so failing commands would still be ignored.

  • ✓

    set -e

    Why this is correct

    set -e makes the shell terminate immediately when any command returns a non-zero exit status, satisfying the requirement to stop on failure. Without it, Bash continues executing subsequent commands by default, masking the error and allowing the script to run on with corrupted state.

  • ✗

    set -x

    Why it's wrong here

    set -x enables tracing, printing each command with its expanded arguments to stderr for debugging; it changes no exit behaviour. The scenario requires set -e, which exits immediately when a command returns non-zero. Tracing is tempting during script development, but it is a diagnostic aid, not an error-handling directive.

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.