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"?
ARemoves the longest prefix ending with the last slash, resulting in <code>bin</code>
BRemoves the shortest prefix ending with the first slash, resulting in <code>usr/local/bin</code>
CRemoves all slashes from the string
DRemoves the first character only
✗ 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?
A${var%pattern}
B${var#pattern}
C${var##pattern}
D${var%%pattern}
✗ Incorrect
${var##pattern} removes the longest matching prefix, while ${var#pattern} removes the shortest.
If var="abc123abc", what is the result of ${var#abc}?
A123abc
Babc123abc
C123
Dbc123abc
✗ 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}?
AThe pattern is removed from anywhere in the string
BThe entire string is removed
CAn error is thrown
DThe string remains unchanged
✗ 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?
AExtracting filename from a full path
BReplacing text inside a string
CRemoving file extensions
DCounting characters in a string
✗ 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.
Practice
(1/5)
1. What does the Bash expression ${var#pattern} do?
easy
A. Removes the shortest matching prefix pattern from the variable's value
B. Removes the longest matching suffix pattern from the variable's value
C. Removes the shortest matching suffix pattern from the variable's value
D. Removes the longest matching prefix pattern from the variable's value
Solution
Step 1: Understand the syntax
The syntax ${var#pattern} is used in Bash to remove a prefix pattern from the start of the variable's value.
Step 2: Identify shortest prefix removal
The single # removes the shortest matching prefix, unlike ## which removes the longest.
Final Answer:
Removes the shortest matching prefix pattern from the variable's value -> Option A
2. Which of the following is the correct syntax to remove the shortest prefix matching abc from variable var in Bash?
easy
A. ${var#abc}
B. ${var##abc}
C. ${var%abc}
D. ${var%%abc}
Solution
Step 1: Recall prefix removal syntax
Single # removes shortest prefix, double ## removes longest prefix. % and %% remove suffixes.
Step 2: Match the pattern
To remove shortest prefix matching 'abc', use ${var#abc}.
Final Answer:
${var#abc} -> Option A
Quick Check:
Shortest prefix removal = single # = B [OK]
Hint: Use single # for shortest prefix removal syntax [OK]
Common Mistakes:
Using ## instead of # for shortest prefix
Using % or %% which remove suffixes
Confusing prefix and suffix removal syntax
3. Given var="folder/subfolder/file.txt", what is the output of echo ${var#*/}?
medium
A. folder/subfolder/file.txt
B. subfolder/file.txt
C. file.txt
D. folder
Solution
Step 1: Understand the pattern */
The pattern */ matches the shortest prefix ending with a slash.
Step 2: Remove shortest prefix matching */
From "folder/subfolder/file.txt", the shortest prefix matching */ is "folder/". Removing it leaves "subfolder/file.txt".
Final Answer:
subfolder/file.txt -> Option B
Quick Check:
Remove shortest prefix ending with / = subfolder/file.txt [OK]
Hint: Pattern */ removes up to first slash [OK]
Common Mistakes:
Removing longest prefix with ## instead of #
Expecting only filename instead of subfolder/file.txt
Confusing prefix removal with suffix removal
4. Consider the script snippet:
var="abc123abc456"
echo ${var##abc*}
It outputs an empty line. Why?
medium
A. Because the pattern is invalid and causes an error
B. Because abc* matches the entire string as shortest prefix
C. Because abc* matches the longest prefix, so nothing remains
D. Because the variable is empty
Solution
Step 1: Analyze the pattern abc*
The pattern abc* matches 'abc' followed by zero or more any characters.
Step 2: Understand longest prefix removal
With ##, it removes the longest matching prefix. The * matches greedily as many characters as possible after 'abc', so the match covers the entire string.
Final Answer:
Because abc* matches the longest prefix, so nothing remains -> Option C
Quick Check:
Longest prefix with ## + greedy * = B [OK]
Hint: Wildcard * matches greedily with longest prefix ## [OK]
Common Mistakes:
Confusing ## (longest) with # (shortest)
Underestimating greedy nature of * in longest match
Assuming pattern causes error or variable is empty
5. You have a variable path="folder/home/user/docs/report.txt". Which command removes only the first slash and everything before it, leaving home/user/docs/report.txt?
hard
A. echo ${path#/*}
B. echo ${path##/*}
C. echo ${path##*/}
D. echo ${path#*/}
Solution
Step 1: Understand the variable value
The variable path is "folder/home/user/docs/report.txt".
Step 2: Analyze each option's pattern
${path#/*} removes shortest prefix matching slash followed by anything, but path doesn't start with /, so no match. ${path##/*} removes longest prefix matching slash-anything, no match. ${path##*/} removes longest prefix up to last slash, leaving only filename "report.txt". ${path#*/} removes shortest prefix up to first slash ("folder/"), leaving "home/user/docs/report.txt".
Step 3: Choose correct option
To remove up to the first slash, use ${path#*/}.
Final Answer:
echo ${path#*/} -> Option D
Quick Check:
Remove shortest prefix up to first slash = D [OK]
Hint: Use #*/ to remove up to first slash only [OK]
Common Mistakes:
Using ##*/ removes up to last slash (only filename)
Using #/* or ##/* which don't match without leading slash
Confusing prefix removal with suffix removal
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.