0
0
Bash Scriptingscripting~20 mins

String suffix removal (${var%pattern}) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Suffix Removal Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
1: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}
Areport_final_v2
Breport_final_v2.txt
Creport_final_v2.tx
Dreport_final_v2.t
Attempts:
2 left
💡 Hint
The % operator removes the shortest matching suffix pattern from the variable's value.
💻 Command Output
intermediate
1: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.*}
A/home/user/docs/file_backup
B/home/user/docs/file_backup.tar
C/home/user/docs/file_backup.tar.gz
D/home/user/docs/file_backup.tar.g
Attempts:
2 left
💡 Hint
The pattern %.tar.* removes the shortest suffix starting with '.tar.'
📝 Syntax
advanced
1: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"
Aecho ${file%\.log}
Becho ${file%.log}
Cecho ${file%log.}
Decho ${file%*.log}
Attempts:
2 left
💡 Hint
The pattern must match the suffix exactly as it appears.
🔧 Debug
advanced
2: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}
ABecause %.tar.gz removes the shortest suffix matching '.tar.gz', but the shell treats '.' as a wildcard and fails.
BBecause %.tar.gz removes only the shortest suffix, but '.tar.gz' is the entire suffix so it matches and removes it.
CBecause %.tar.gz removes the shortest suffix matching '.tar.gz', but the pattern must be quoted to work.
DBecause %.tar.gz removes the shortest suffix matching '.tar.gz', but the pattern is treated literally and does not match the suffix.
Attempts:
2 left
💡 Hint
The pattern must match some suffix of the variable exactly.
🚀 Application
expert
2: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"
Aecho ${file#*.}
Becho ${file%.*}
Cecho ${file%%.*}
Decho ${file##*.}
Attempts:
2 left
💡 Hint
Use %% to remove the longest matching suffix pattern.