What does the syntax ${var%pattern} do in bash scripting?
It removes the shortest matching suffix pattern from the value of the variable var. This means it deletes the smallest part at the end of the string that matches pattern.
Click to reveal answer
intermediate
How does ${var%%pattern} differ from ${var%pattern}?
${var%%pattern} removes the longest matching suffix pattern, while ${var%pattern} removes the shortest matching suffix pattern from the variable's value.
Click to reveal answer
beginner
Example: If file="report.txt.bak", what is the result of ${file%.bak}?
The result is report.txt. It removes the shortest suffix matching .bak from the variable file.
Click to reveal answer
intermediate
Why use string suffix removal instead of external commands like sed or cut?
Because string suffix removal is faster and uses built-in shell features, so it avoids starting extra programs. It is simpler and more efficient for small string changes.
Click to reveal answer
beginner
What happens if the pattern does not match any suffix in ${var%pattern}?
If the pattern does not match the end of the string, the variable's value remains unchanged.
Click to reveal answer
What does ${filename%.txt} do if filename="notes.txt"?
ADoes nothing
BRemoves the prefix '.txt' from filename
CRemoves all '.txt' occurrences in filename
DRemoves the suffix '.txt' from filename
✗ Incorrect
It removes the shortest suffix matching '.txt' from the variable's value.
Which syntax removes the longest matching suffix pattern in bash?
A${var%%pattern}
B${var#pattern}
C${var%pattern}
D${var##pattern}
✗ Incorrect
${var%%pattern} removes the longest matching suffix pattern.
If path="folder/file.tar.gz", what does ${path%.gz} return?
Afolder/file.tar.gz
Bfolder/file.tar.g
Cfolder/file.tar
Dfolder/file.tar.gz.gz
✗ Incorrect
It removes the shortest suffix '.gz' from the string.
What happens if the pattern does not match the suffix in ${var%pattern}?
AThe variable is emptied
BThe variable value is unchanged
CAn error occurs
DThe entire string is removed
✗ Incorrect
If no match, the original string remains unchanged.
Why is using ${var%pattern} preferred over external tools for suffix removal?
AIt is built-in and faster
BIt requires installing extra software
CIt is slower but more readable
DIt works only on Linux
✗ Incorrect
Built-in string manipulation is faster and does not require extra programs.
Explain how to remove a file extension suffix from a filename variable in bash using string suffix removal.
Think about removing the shortest suffix matching the extension.
You got /3 concepts.
Describe the difference between ${var%pattern} and ${var%%pattern} in bash string manipulation.
One removes the smallest matching suffix, the other the largest.
You got /2 concepts.
Practice
(1/5)
1. What does the Bash expression ${filename%.txt} do to the variable filename?
easy
A. Removes the shortest prefix matching '.txt' from the start of filename
B. Removes the longest suffix matching '.txt' from the end of filename
C. Removes the shortest suffix matching '.txt' from the end of filename
D. Removes the longest prefix matching '.txt' from the start of filename
Solution
Step 1: Understand the % operator in Bash
The single % operator removes the shortest matching suffix pattern from the variable's value.
Step 2: Apply to the pattern '.txt'
It removes '.txt' only if it appears at the end of the string, trimming that suffix.
Final Answer:
Removes the shortest suffix matching '.txt' from the end of filename -> Option C
You want to remove the '.csv' suffix only from files that end with '.csv' using Bash parameter expansion. Which command correctly prints the filenames with '.csv' removed where applicable?
hard
A. for f in "${files[@]}"; do echo ${f%.csv}; done
B. for f in "${files[@]}"; do echo ${f%%csv}; done
C. for f in "${files[@]}"; do echo ${f%csv}; done
D. for f in "${files[@]}"; do echo ${f#%.csv}; done
Solution
Step 1: Understand the goal
Remove '.csv' suffix only if it exists, leave other filenames unchanged.
Step 2: Check each option's effect
A uses single % with '.csv' pattern, removing suffix if present. B uses %%csv missing the dot, incorrect. C misses the dot, incorrect. D uses # which removes prefix, wrong direction.
Step 3: Choose best option
for f in "${files[@]}"; do echo ${f%.csv}; done is correct and simplest for shortest suffix removal of '.csv'.
Final Answer:
for f in "${files[@]}"; do echo ${f%.csv}; done -> Option A
Quick Check:
Use ${var%.csv} to remove '.csv' suffix if present [OK]
Hint: Use ${var%.ext} in loop to trim suffix conditionally [OK]
Common Mistakes:
Using prefix removal (#) instead of suffix removal (%)
Omitting the dot in suffix pattern
Using %% unnecessarily when % suffices
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.