Courseiva
easyMultiple Choice

XK0-006 Practice Question: A bash script uses a for loop to iterate over…

A bash script uses a for loop to iterate over files in a directory. Which of the following correctly assigns each filename to the variable FILE?

⚠ Common exam trap

Watch out — candidates often choose `$(ls *.txt)` (Option A) because they think they need to explicitly list files with `ls`, not realizing that the shell's built-in globbing is safer and more efficient, and that `ls` output parsing is fragile.

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

The shell expands the wildcard pattern `*.txt` into a list of matching filenames before the `for` loop executes, and each filename is assigned to the variable `FILE` in turn. This approach is safe and efficient because it avoids parsing the output of `ls`, which can break with filenames containing spaces or special characters.

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 $(ls *.txt); do

    Why it's wrong here

    Command substitution via $(ls *.txt) word-splits on whitespace, so filenames containing spaces are broken into separate iterations, and ls output is fragile. It is tempting because it appears to enumerate matching files, but glob expansion (for FILE in *.txt) handles filenames safely without invoking ls.

  • ✓

    for FILE in *.txt; do

    Why this is correct

    The glob *.txt undergoes pathname expansion, so the for loop assigns each matching filename in the current directory to FILE on successive iterations. Quoting the pattern or using command substitution would break that per-file assignment.

  • ✗

    for FILE in 'ls *.txt'; do

    Why it's wrong here

    Single quotes prevent command substitution, so the loop iterates once over the literal string 'ls *.txt' rather than filenames. It is tempting because it looks like it runs ls, but quoting suppresses execution; unquoted $(ls *.txt) or a glob would actually expand to the file list.

  • ✗

    for FILE = *.txt; do

    Why it's wrong here

    Spaces around the equals sign make bash parse FILE as a command word, and *.txt undergoes glob expansion before assignment, so the syntax is invalid. It is tempting because it resembles variable assignment, but a for loop requires 'for FILE in *.txt; do' with no spaces around = and the in keyword.

About these practice questions

This XK0-006 question is part of Courseiva's 781-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam 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.