Courseiva
Create simple shell scriptseasyMultiple ChoiceObjective-mapped

EX200 Create simple shell scripts Practice Question

Exhibit

Refer to the exhibit.
```bash
#!/bin/bash
# Script to count lines in files
for file in *.txt; do
  wc -l "$file"
done
```

Consider the script in the exhibit. The script is run in a directory containing 'a.txt' and 'b.txt' but also has a subdirectory 'backup' with .txt files. What will be the output?

⚠ Common exam trap

Red Hat often tests the candidate's understanding that shell glob patterns like `*.txt` do not recurse into subdirectories, leading many to incorrectly assume that all .txt files in the entire directory tree are processed.

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

Line counts for 'a.txt' and 'b.txt' only

The script uses `for i in *.txt`, which by default only matches .txt files in the current directory, not in subdirectories. Since 'a.txt' and 'b.txt' are in the current directory, the loop iterates over them and runs `wc -l` on each, outputting their line counts. The 'backup' subdirectory is not traversed because the glob pattern does not include paths with directories.

Answer analysis

Option-by-option breakdown

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

  • An error because the for loop cannot iterate over files with spaces

    Why it's wrong here

    The for loop itself does not error when filenames contain spaces; the shell performs glob expansion before the loop starts and yields each matching filename as a separate argument. In this particular environment there are no `.txt` files with spaces, so the loop will run normally. Even if such files existed, a robust script quoting the expansion with `"$i"` would still work correctly, so an error claim is unfounded.

  • Line counts for .txt files in 'backup' only

    Why it's wrong here

    The glob pattern `*.txt` is expanded by the shell in the current working directory only; it does not traverse into the `backup` directory. Since the script never uses `find` or a recursive glob (`**/*.txt`), only current-directory files are processed, making the 'backup only' outcome impossible. Additionally, the files in `backup` would not be counted at all, so selecting only those cannot happen.

  • Line counts for 'a.txt' and 'b.txt' only

    Why this is correct

    The shell expands `*.txt` to the names of matching files in the current directory, which in the given directory listing are `a.txt` and `b.txt`. The `for i in` loop assigns each name in turn to `i`, and `wc -l "$i"` prints the line count for each file. Files in subdirectories such as `backup` are not matched because globbing is not recursive unless `**` is used.

  • Line counts for all .txt files including those in 'backup'

    Why it's wrong here

    This answer reflects the misconception that `*.txt` recurses into subdirectories. Shell globbing is non-recursive: `*` matches any characters within a single path component, so it expands only to filenames in the current directory, not to paths containing a slash. Because the loop iterates over the expanded list, none of the `.txt` files inside `backup/` are included in the output.

About these practice questions

This EX200 question is part of Courseiva's 127-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 EX200 practice question is part of Courseiva's free Red Hat 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 EX200 exam.