Courseiva
Shells, Scripting and Data ManagementeasyMultiple ChoiceObjective-mapped

LPIC-1 Shells, Scripting and Data Management Practice Question

Which sed command will replace the first occurrence of 'foo' with 'bar' on each line of a file?

⚠ Common exam trap

It's easy for candidates to confuse the default behavior of sed's substitute command, assuming it replaces all occurrences unless told otherwise, and thus incorrectly choose the `g` flag option.

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

sed 's/foo/bar/' file

The default behavior of the `s` (substitute) command in sed is to replace only the first occurrence of the pattern on each line. Without a numeric flag, `sed 's/foo/bar/' file` replaces the first 'foo' on each line with 'bar'. The `g` flag would replace all occurrences, not just the first.

Answer analysis

Option-by-option breakdown

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

  • sed 's/foo/bar/g' file

    Why it's wrong here

    The g flag replaces all occurrences.

  • sed 's/foo/bar/0' file

    Why it's wrong here

    Invalid flag 0; sed would error.

  • sed 's/foo/bar/2' file

    Why it's wrong here

    Replaces the second occurrence per line.

  • sed 's/foo/bar/' file

    Why this is correct

    Default replaces first occurrence per line.

About these practice questions

Courseiva writes every LPIC-1 question from scratch — 527 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or 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 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.