Courseiva
hardMultiple SelectObjective-mapped

XK0-006 Practice Question: A developer is writing a shell script that needs…

A developer is writing a shell script that needs to handle errors. Which THREE of the following are best practices for robust script error handling?

⚠ Common exam trap

CompTIA often tests the distinction between `set -e` and `set +e` and the proper use of `$?` versus relying solely on `set -e`, where candidates may incorrectly think that `set +e` is a best practice for error handling when it actually disables automatic exit on error.

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 code of commands using $?

Checking the exit code of commands using `$?` allows the script to conditionally handle failures based on the specific return value of each command. This is a fundamental error-handling technique in shell scripting, as every command returns an exit code (0 for success, non-zero for failure), and inspecting `$?` immediately after a command lets the developer decide how to respond to that specific error.

Answer analysis

Option-by-option breakdown

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

  • Ignore errors to simplify the script

    Why it's wrong here

    Not robust.

  • Check the exit code of commands using $?

    Why this is correct

    Allows conditional handling.

  • Use set +e to allow the script to continue on error

    Why it's wrong here

    Opposite of best practice.

  • Use set -e to exit on any command failure

    Why this is correct

    Stops script on error.

  • Use trap to catch signals and clean up

    Why this is correct

    Ensures cleanup on exit.

About these practice questions

Courseiva writes every XK0-006 question from scratch — 979 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.