Bird
0
0

Consider this Bash snippet:

medium📝 Debug Q14 of 15
Bash Scripting - String Operations
Consider this Bash snippet:
file="archive.tar.gz"
echo ${file%*.gz}

It outputs archive.tar. You want to remove the entire suffix .tar.gz. What is the fix?
AUse ${file%%*.gz} to remove the longest matching suffix
BUse ${file%.gz} to remove only '.gz' suffix
CUse ${file#*.tar.gz} to remove prefix instead
DUse ${file%.tar} to remove only '.tar' suffix
Step-by-Step Solution
Solution:
  1. Step 1: Understand why ${file%*.gz} removes only '.gz'

    Single % removes shortest suffix matching *.gz pattern, so it matches '.gz' (with * matching empty).
  2. Step 2: Use %% to remove longest suffix

    Double %% removes longest matching suffix, so ${file%%*.gz} removes entire '.tar.gz'.
  3. Final Answer:

    Use ${file%%*.gz} to remove the longest matching suffix -> Option A
  4. Quick Check:

    %% removes longest suffix, % removes shortest [OK]
Quick Trick: Use %% to remove longest suffix pattern [OK]
Common Mistakes:
MISTAKES
  • Using single % when longest suffix removal needed
  • Confusing prefix removal (#) with suffix removal (%)
  • Trying to remove partial suffix only

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Bash Scripting Quizzes