Recall & Review
beginner
What does the syntax
${var#pattern} do in bash scripting?It removes the shortest matching prefix pattern from the value of the variable
var. This means it deletes the smallest part at the start of the string that matches pattern.Click to reveal answer
intermediate
How is
${var##pattern} different from ${var#pattern}?${var##pattern} removes the longest matching prefix pattern, while ${var#pattern} removes the shortest matching prefix pattern from the variable's value.Click to reveal answer
beginner
Example: Given
file="/home/user/docs/report.txt", what is the result of ${file#*/}?It removes the shortest prefix matching
*/, which is / up to and including the first slash. Result: home/user/docs/report.txt.Click to reveal answer
beginner
Why use string prefix removal in bash scripts?
It helps to extract parts of strings by cutting off unwanted beginnings, like removing directory paths or prefixes, making scripts cleaner and easier to read.
Click to reveal answer
beginner
What happens if the pattern does not match any prefix in
${var#pattern}?The variable's value remains unchanged because no matching prefix is found to remove.
Click to reveal answer
What does
${var#*/} do to the string path="/usr/local/bin"?✗ Incorrect
${var#*/} removes the shortest prefix matching */, which is everything up to and including the first slash.Which syntax removes the longest matching prefix pattern in bash?
✗ Incorrect
${var##pattern} removes the longest matching prefix, while ${var#pattern} removes the shortest.If
var="abc123abc", what is the result of ${var#abc}?✗ Incorrect
The shortest prefix matching
abc is removed, leaving 123abc.What happens if the pattern does not match the start of the string in
${var#pattern}?✗ Incorrect
If the pattern does not match the start, no removal happens and the string stays the same.
Which of these is a practical use of
${var#pattern} in scripts?✗ Incorrect
Removing the shortest prefix can extract the filename by cutting off the directory path.
Explain how
${var#pattern} works in bash scripting and give a simple example.Think about cutting off the start of a string that matches a pattern.
You got /3 concepts.
Describe the difference between
${var#pattern} and ${var##pattern} and when you might use each.One removes the smallest match, the other the biggest match at the start.
You got /3 concepts.