EX200 Create simple shell scripts Practice Question
A script needs to read every line from a file and execute a command on each line. Which code block is correct and handles whitespace correctly?
⚠ Common exam trap
Red Hat often tests the distinction between `while read` loops and `for` loops with command substitution, trapping candidates who forget that `for line in $(cat file)` splits on all whitespace and expands globs, not just newlines.
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
✓
while IFS= read -r line; do echo "$line"; done < file.txt
It uses `IFS=` to preserve leading/trailing whitespace and `-r` to prevent backslash interpretation, ensuring each line is read exactly as it appears in the file. The `< file.txt` redirect feeds the file line by line into the `while` loop, which is the standard and safe method for line-by-line processing in Bash.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
while IFS=$'\n' read line; do echo "$line"; done <<< file.txt
Why it's wrong here
This uses a here-string (<<<), which passes the literal string 'file.txt' as standard input rather than opening the file's contents, so the loop reads one line consisting of the filename itself. Since the loop never sees the actual file data, it will echo 'file.txt' once instead of processing each line. Setting IFS=$'\n' also does not help: read still splits on any IFS whitespace and lacks -r, leaving backslash handling to the shell.
- ✓
while IFS= read -r line; do echo "$line"; done < file.txt
Why this is correct
Setting IFS= ensures read does not trim leading or trailing whitespace from each line, preserving the exact content of the input. The -r flag prevents backslashes from being interpreted as escape characters, so paths like C:\new remain literal. Quoting "$line" in the echo command prevents word splitting and glob expansion, and < file.txt correctly redirects the file to the loop's stdin line by line. This combination is the canonical robust way to process lines in Bash.
- ✗
while read line; do echo $line; done < file.txt
Why it's wrong here
Without -r, read treats backslash escape sequences as control characters, so data containing backslashes is silently modified. Then unquoted $line in the echo command undergoes word splitting on spaces/tabs and pathname expansion, so lines with multiple words or glob characters like * are broken apart or expanded. The default IFS behavior of read also eats leading/trailing whitespace, making this version unreliable for faithful line processing.
- ✗
for line in $(cat file.txt); do echo $line; done
Why it's wrong here
Command substitution $(cat file.txt) first splits the entire file on the shell's IFS (spaces, tabs, and newlines), so every word becomes a separate iteration instead of every line. Any glob characters, such as *, are subject to pathname expansion against the current directory, potentially matching unintended filenames. The unquoted echo $line compounds the problem by splitting each iteration's content again, making this both fragile and inefficient compared to a read-based loop.
Go deeper
Related to this question
About these practice questions
This EX200 question is part of Courseiva's 427-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 →
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.