Challenge - 5 Problems
Suffix Removal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate1:30remaining
What is the output of this suffix removal?
Given the variable assignment
filename="report_final_v2.txt", what is the output of echo ${filename%.txt}?Bash Scripting
filename="report_final_v2.txt"
echo ${filename%.txt}Attempts:
2 left
💡 Hint
The % operator removes the shortest matching suffix pattern from the variable's value.
✗ Incorrect
The pattern %.txt removes the shortest suffix matching '.txt' from the variable, so the output is 'report_final_v2'.
💻 Command Output
intermediate1:30remaining
Removing suffix with wildcard pattern
What is the output of this command?
path="/home/user/docs/file_backup.tar.gz"
echo ${path%.tar.*}Bash Scripting
path="/home/user/docs/file_backup.tar.gz"
echo ${path%.tar.*}Attempts:
2 left
💡 Hint
The pattern %.tar.* removes the shortest suffix starting with '.tar.'
✗ Incorrect
The pattern %.tar.* removes the shortest suffix matching '.tar.' followed by anything, so it removes '.tar.gz' from the end.
📝 Syntax
advanced1:30remaining
Identify the correct suffix removal syntax
Which option correctly removes the shortest suffix matching '.log' from the variable
file?Bash Scripting
file="system_error.log"Attempts:
2 left
💡 Hint
The pattern must match the suffix exactly as it appears.
✗ Incorrect
Option B correctly uses %.log to remove the suffix '.log'. Option B uses wrong order, A escapes unnecessarily, D uses wildcard incorrectly.
🔧 Debug
advanced2:00remaining
Why does this suffix removal fail?
Given
name="archive.tar.gz.bak", why does echo ${name%.tar.gz} output archive.tar.gz.bak instead of archive?Bash Scripting
name="archive.tar.gz.bak"
echo ${name%.tar.gz}Attempts:
2 left
💡 Hint
The pattern must match some suffix of the variable exactly.
✗ Incorrect
The pattern %.tar.gz requires a suffix that exactly matches '.tar.gz'. Here, the value ends with '.tar.gz.bak', which does not have a suffix matching '.tar.gz', so nothing is removed. Dots are literal in these glob patterns.
🚀 Application
expert2:00remaining
Extract base filename without extension using suffix removal
You have a variable
file="backup_2024-06-01.tar.gz". Which command correctly extracts the base filename backup_2024-06-01 by removing the longest suffix starting with the first dot?Bash Scripting
file="backup_2024-06-01.tar.gz"Attempts:
2 left
💡 Hint
Use %% to remove the longest matching suffix pattern.
✗ Incorrect
Option C uses %%.* to remove the longest suffix starting with a dot, leaving the base filename. Option C removes only the shortest suffix after the last dot, leaving 'backup_2024-06-01.tar'. Options C and D remove prefixes, not suffixes.