Courseiva
Shells, Scripting and Data ManagementhardMultiple ChoiceObjective-mapped

LPIC-1 Shells, Scripting and Data Management Practice Question

A script uses a while loop to read lines from a file, but a variable set inside the loop is empty after the loop finishes. What is the most likely cause?

⚠ Common exam trap

The trap here is that candidates often overlook the subshell behavior of pipelines and incorrectly attribute the issue to file content or variable declaration errors, rather than recognizing that the pipeline creates a separate shell environment.

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 while loop is part of a pipeline, causing it to run in a subshell.

When a while loop is part of a pipeline (e.g., `while read line; do ... done < file | command` or `command | while read line; do ... done`), each command in the pipeline runs in its own subshell. Variables set inside the subshell are not propagated back to the parent shell, so they appear empty after the loop finishes. This is a common pitfall in shell scripting.

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 file has an empty line at the end.

    Why it's wrong here

    Would not cause variable to be empty after loop.

  • The variable is declared as readonly.

    Why it's wrong here

    Would cause an error, not just empty.

  • The variable name contains spaces.

    Why it's wrong here

    Variable names cannot contain spaces.

  • The while loop is part of a pipeline, causing it to run in a subshell.

    Why this is correct

    Pipeline subshells isolate variable changes.

About these practice questions

One of 527 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 →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

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.