Courseiva

XK0-006 Automation, Orchestration, and Scripting Practice Question

A Linux administrator is troubleshooting a Bash script that unexpectedly terminates when a command fails. The script uses `#!/bin/bash`. Which of the following commands, if placed at the beginning of the script, would cause it to exit on any command failure?

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` option makes the shell exit immediately if a command exits with a non-zero status.

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 -u

    Why it's wrong here

    set -u aborts only on expansion of unset variables; a failing command returns non-zero without triggering exit, so the script continues. It is tempting because it is a common hardening directive placed at script start, and would be correct when the requirement is to catch undefined variable references rather than command failures.

  • ✗

    trap 'exit 1' ERR

    Why it's wrong here

    The ERR trap fires only on non-zero exit statuses, but without set -e the shell continues executing subsequent commands, so the script does not terminate. It is tempting because trap is genuinely used for cleanup handlers and custom error logging, and would suit scenarios requiring bespoke failure actions rather than immediate exit.

  • ✓

    set -e

    Why this is correct

    `set -e` makes Bash exit immediately when any command returns a non-zero status, which is exactly the failure-termination behaviour described. The shebang already invokes Bash, so this shell option applies. It satisfies the requirement to exit on any command failure.

  • ✗

    set -o pipefail

    Why it's wrong here

    pipefail only changes a pipeline's exit status to reflect a failing component; it does not terminate the script on failure. It is tempting because it is often paired with errexit in robust scripts, but alone it merely propagates pipeline failures to the shell's return code.

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 by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.