Courseiva
Shells, Scripting and Data ManagementmediumMultiple ChoiceObjective-mapped

LPIC-1 Shells, Scripting and Data Management Practice Question

In a bash script, a variable 'file' is set to '/var/log/syslog'. Which expansion will yield the string '/var/log'?

⚠ Common exam trap

Candidates often confuse the `%` (remove suffix) and `#` (remove prefix) operators, or mistakenly think `%%` removes the last path component when it actually removes everything from the first slash onward due to longest match behavior.

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

${file%/*}

The `${file%/*}` expansion uses the `%` operator to remove the shortest suffix matching the pattern `/*`, which matches the last slash and everything after it. Since `file` is `/var/log/syslog`, this removes `/syslog`, leaving `/var/log`. This is a standard parameter expansion in bash for stripping a trailing path component.

Answer analysis

Option-by-option breakdown

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

  • ${file##*/}

    Why it's wrong here

    Uses ${file##*/} which removes the longest prefix matching '*/', yielding 'syslog'.

  • ${file%%/*}

    Why it's wrong here

    Uses ${file%%/*} which removes the longest suffix matching '/*', yielding an empty string.

  • ${file%/*}

    Why this is correct

    Ly uses ${file%/*} to remove the shortest suffix matching '/*', giving '/var/log'.

  • ${file#*/}

    Why it's wrong here

    Uses ${file#*/} which removes the shortest prefix matching '*/', yielding 'var/log/syslog'.

About these practice questions

This LPIC-1 question is part of Courseiva's 527-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 →

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.