Bird
0
0

Why does ${var#pattern} remove only the shortest matching prefix and not the longest?

hard🧠 Conceptual Q10 of 15
Bash Scripting - String Operations
Why does ${var#pattern} remove only the shortest matching prefix and not the longest?
ABecause '#' always removes suffix, not prefix
BBecause '#' removes the entire variable value
CBecause pattern matching is disabled by default
DBecause single '#' matches shortest prefix, double '##' matches longest prefix
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between # and ##

    Single '#' removes shortest prefix matching pattern, double '##' removes longest prefix.
  2. Step 2: Confirm behavior of '#' in Bash

    This is standard Bash parameter expansion behavior for prefix removal.
  3. Final Answer:

    Because single '#' matches shortest prefix, double '##' matches longest prefix -> Option D
  4. Quick Check:

    # = shortest prefix, ## = longest prefix [OK]
Quick Trick: Single # shortest prefix, double ## longest prefix [OK]
Common Mistakes:
MISTAKES
  • Thinking # removes suffix
  • Believing pattern matching is off
  • Assuming # removes entire variable

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes