Courseiva
Automation, Orchestration, and ScriptingmediumMultiple SelectObjective-mapped

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).

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

for file in *.txt expands to all matching filenames; the C-style for loop can also be used with array of filenames, but direct expansion is simpler.

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 prevent glob expansion; loops over literal string '*.txt'.

  • for file in $(ls *.txt); do

    Why it's wrong here

    Parsing ls is error-prone; may break with spaces.

  • for file in *.txt; do

    Why this is correct

    Glob expands to all .txt files.

  • for ((i=0; i<${#files[@]}; i++)); do

    Why it's wrong here

    This requires an array variable 'files' to be defined first.

  • while IFS= read -r file; do done < <(find . -maxdepth 1 -name '*.txt')

    Why this is correct

    Process substitution with find is safe for filenames with spaces.

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.