0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Prefix Removal Pro
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 prefix removal?
Given the variable file="backup_2024_report.txt", what is the output of echo ${file#backup_}?
Bash Scripting
file="backup_2024_report.txt"
echo ${file#backup_}
Areport.txt
Bbackup_2024_report.txt
C_2024_report.txt
D2024_report.txt
Attempts:
2 left
💡 Hint
The # operator removes the shortest match of the pattern from the start of the string.
💻 Command Output
intermediate
1:30remaining
Removing shortest vs longest prefix
Given path="/home/user/docs/file.txt", what is the output of echo ${path##*/}?
Bash Scripting
path="/home/user/docs/file.txt"
echo ${path##*/}
Afile.txt
B/file.txt
Cdocs/file.txt
D/home/user/docs/file.txt
Attempts:
2 left
💡 Hint
The ## operator removes the longest match of the pattern from the start.
🔧 Debug
advanced
2:00remaining
Why does this prefix removal fail?
Consider var="version1.2.3". What error or output results from echo ${var#version.}?
Bash Scripting
var="version1.2.3"
echo ${var#version.}
A1.2.3
Bversion1.2.3
CSyntax error
Dersion1.2.3
Attempts:
2 left
💡 Hint
The pattern must match exactly at the start to be removed.
🧠 Conceptual
advanced
1:30remaining
What does this prefix removal pattern do?
Given text="error_404_not_found", what is the output of echo ${text#*_}?
Bash Scripting
text="error_404_not_found"
echo ${text#*_}
A404_not_found
Berror_404_not_found
Cnot_found
D_404_not_found
Attempts:
2 left
💡 Hint
The pattern '*_' removes the shortest prefix ending with underscore.
🚀 Application
expert
2:30remaining
Extract filename without extension using prefix removal
Given filename="archive.tar.gz", which command outputs tar.gz by removing the shortest prefix ending with a dot?
Bash Scripting
filename="archive.tar.gz"
echo ???
Aecho ${filename#*.tar}
Becho ${filename##*.}
Cecho ${filename#*.}
Decho ${filename#archive.}
Attempts:
2 left
💡 Hint
Use # to remove shortest prefix matching pattern including a dot.