LPIC-1 Shells, Scripting and Data Management Practice Question
A script uses the command substitution: files=$(ls). Which statement about the resulting value of files is true?
⚠ Common exam trap
The trap here is thinking that command substitution replaces all newlines with spaces or that it captures the exit status rather than the command's output.
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
✓
The variable files contains the output of ls, with trailing newlines removed.
Command substitution captures the standard output of the enclosed command and removes trailing newline characters. Internal newlines are retained, but the final newline is stripped. This behavior is consistent across shells and is why using the result unquoted can lead to word splitting on spaces, tabs, and newlines.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✗
The variable files contains the output of ls with newlines replaced by spaces.
Why it's wrong here
Command substitution preserves newlines in the captured output; it does not replace them with spaces. However, when the variable is later used unquoted, word splitting may treat newlines as separators. The assignment itself retains the newlines, so this statement is not accurate.
- ✗
The variable files contains the exit status of the ls command.
Why it's wrong here
Command substitution captures standard output, not the exit status. The exit status of ls is available through the special parameter $? immediately after the command substitution completes. The variable files would hold the textual output, not a numeric status code.
- ✓
The variable files contains the output of ls, with trailing newlines removed.
Why this is correct
Command substitution captures the standard output of the command and strips any trailing newline characters. Internal newlines remain, but the final newline that ls typically outputs is removed. Therefore the variable holds the output with trailing newlines stripped, which is the documented behavior.
- ✗
The variable files contains a list of filenames separated by null characters.
Why it's wrong here
Command substitution captures text output, not null-separated data. The ls command outputs filenames separated by newlines by default, not null characters. To obtain null-separated output, one would use find with -print0 or ls with specific options, but that is not what this syntax does.
Go deeper
Related to this question
About these practice questions
One of 402 original LPIC-1 practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. 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 LPI exam blueprint
This LPIC-1 practice question is part of Courseiva's free LPI 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 LPIC-1 exam.