Courseiva
Shells, Scripting and Data ManagementmediumMultiple ChoiceObjective-mapped

LPIC-1 read command Practice Question

Exhibit

Consider the following script named 'parse.sh':

#!/bin/bash
while IFS=',' read -r field1 field2 remainder; do
	echo "$field1 $field2"
done < input.txt

Assume input.txt contains:
a,b,c
d,e,f

Refer to the exhibit. What will be the output when this script is executed?

⚠ Common exam trap

The trap is that `read` assigns the remainder of the line to the last variable when there are more words than variables. Candidates often think each variable gets exactly one word, but actually the last variable absorbs all remaining words.

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

a b c\nd e f

The script reads two lines from standard input. The first `read a b` reads the first line 'a b c'. Since there are three words but only two variables, the last variable `b` captures the remainder of the line, so a='a' and b='b c'. The second `read d e` reads the second line 'd e f', so d='d' and e='e f'. The `echo -e` command outputs the values with an escaped newline (`\n`) between them, producing two lines: 'a b c' and 'd e f', represented as 'a b c\nd e f'.

Answer analysis

Option-by-option breakdown

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

  • a b\nd e

    Why it's wrong here

    Incorrect. This output would occur if `read` assigned only the first word to each variable and discarded the rest, but the last variable actually captures the remainder.

  • a b c\nd e f

    Why this is correct

    Correct. Because `b` and `e` capture the remaining words from their respective lines, the output includes 'b c' and 'e f'.

  • a b

    Why it's wrong here

    Incorrect. This output would result if the script only printed the first variable from each line, ignoring the rest.

  • The script will error because of incorrect read syntax.

    Why it's wrong here

    Incorrect. The syntax is valid; `read` does not error when there are more words than variables.

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.