XK0-006 Automation, Orchestration, and Scripting Practice Question
A bash script uses a loop to iterate over files in a directory. Which TWO of the following loop constructs will correctly iterate over each .txt file in the current directory? (Select TWO).
⚠ Common exam trap
XK0-006 often tests the difference between quoted and unquoted globs, and the danger of parsing ls output — candidates frequently pick $(ls *.txt) thinking it is equivalent to globbing, when it is actually a word-splitting hazard.
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
✓
for file in *.txt; do
Option C is correct because `for file in *.txt; do` relies on bash pathname expansion (globbing), which expands the unquoted pattern `*.txt` into the list of matching filenames in the current directory, iterating over each one. Option E is correct because `while IFS= read -r file; do ... done < <(find . -maxdepth 1 -name '*.txt')` uses process substitution to feed find's output into the loop; `IFS=` preserves leading/trailing whitespace and `read -r` prevents backslash interpretation, and `find . -maxdepth 1 -name '*.txt'` lists only .txt files directly in the current directory. Option A is wrong because quoting `'*.txt'` suppresses globbing, so the loop runs once with the literal string `*.txt`. Option B is wrong because parsing `ls` output is fragile and breaks on filenames containing spaces, newlines, or glob characters. Option D is wrong because it iterates over indices of a `files` array that is never populated in the scenario, so it does not iterate over the .txt files.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
for file in '*.txt'; do
Why it's wrong here
Single quotes suppress glob expansion, so the loop iterates once over the literal string '*.txt' rather than matching files. It is tempting because the syntax resembles a working glob loop, but unquoted *.txt is required for pathname expansion; quoting suits literal strings, not filename matching.
- ✗
for file in $(ls *.txt); do
Why it's wrong here
Command substitution splits ls output on whitespace, so filenames containing spaces break into separate iterations. It is tempting because it lists matching files and works for simple names, but unquoted expansion causes word splitting; a glob loop handles spaces and newlines correctly.
- ✓
for file in *.txt; do
Why this is correct
Glob expansion by the shell resolves `*.txt` into a list of matching filenames before the loop runs, so each iteration assigns one filename to `file`. This satisfies the requirement to iterate over every .txt file in the current directory, provided matches exist.
- ✗
for ((i=0; i<${#files[@]}; i++)); do
Why it's wrong here
This C-style loop indexes a files array that the stem never populates, so it iterates nothing. It is tempting because indexed iteration is valid bash and useful when an array already holds filenames, but here no array assignment exists; a glob-based for loop is needed instead.
- ✓
while IFS= read -r file; do done < <(find . -maxdepth 1 -name '*.txt')
Why this is correct
Using process substitution, `find . -maxdepth 1 -name '*.txt'` feeds matching filenames into the `while` loop, with `IFS=` and `read -r` preserving whitespace and backslashes. This satisfies the requirement to iterate over each .txt file in the current directory, including names containing spaces.
Go deeper
Related to this question
Learn chapter
Managing Storage and File Systems
Key term
Process
In IT service management, a process is a structured set of activities designed to accomplish a specific objective, such as managing incidents or changes, by transforming inputs into defined outputs.
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.
About these practice questions
Courseiva writes every XK0-006 question from scratch — 781 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 →
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.