mediumMultiple Choice
XK0-006 Practice Question: A systems administrator creates a bash script…
A systems administrator creates a bash script that processes log files. The script uses a for loop to iterate over files in /var/log and runs a command on each. Which of the following would prevent the script from failing if no files match the pattern?
⚠ Common exam trap
Test-takers frequently confuse `nullglob` with `failglob` or assume that `set -e` or `set -u` can handle glob failures, when in fact only `nullglob` prevents the literal pattern string from being passed as an argument, thereby avoiding a 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
✓
shopt -s nullglob
`shopt -s nullglob` causes the shell to expand a glob pattern that matches no files into an empty string rather than leaving the pattern literal. Without this setting, if no files match the pattern in `/var/log`, the for loop receives the literal pattern string (e.g., `*.log`) and attempts to process it as a filename, which would cause the command to fail or produce unexpected results. Enabling nullglob ensures the loop body simply does not execute when no matches exist, preventing script 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 -u
Why it's wrong here
set -u errors on unset variable expansion, which is unrelated to unmatched glob patterns in a for loop. It is tempting because it catches typo'd variable names, and would be correct when the requirement is failing fast on undefined variables rather than on empty directory matches.
- ✗
set -e
Why it's wrong here
set -e aborts the script when a command returns non-zero, which worsens the failure rather than preventing it. It is tempting because it hardens scripts against silent errors, and would be correct when the requirement is stopping execution immediately after any command fails.
- ✗
shopt -s failglob
Why it's wrong here
failglob makes an unmatched pattern raise an error and abort the command, the opposite of tolerating an empty match. It is tempting because it surfaces typos in paths, and would be correct when the requirement is detecting a pattern that should have matched files but did not.
- ✓
shopt -s nullglob
Why this is correct
By default bash leaves an unmatched glob pattern literal, so the loop runs once with the unexpanded string and the command fails. nullglob makes bash expand a non-matching pattern to nothing, so the loop body never executes.
Go deeper
Related to this question
Learn chapter
Basic Shell Scripting
Key term
Bash
Bash is a command-line interpreter that lets users interact with an operating system by typing text commands instead of clicking icons.
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.
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 →
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.