String prefix removal (${var#pattern}) in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to remove a prefix from a string grows as the string gets longer.
Specifically, how does the operation ${var#pattern} behave when the input string changes size?
Analyze the time complexity of the following code snippet.
string="hello_world_example"
pattern="hello_"
result=${string#${pattern}}
echo "$result"
This code removes the shortest matching prefix pattern from the string variable.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The shell checks characters from the start of the string to find the pattern match.
- How many times: It compares characters one by one until the pattern is matched or no match is found.
As the string length grows, the shell compares more characters to find the prefix pattern.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 character checks |
| 100 | About 100 character checks |
| 1000 | About 1000 character checks |
Pattern observation: The number of character comparisons grows roughly in direct proportion to the string length.
Time Complexity: O(n)
This means the time to remove the prefix grows linearly with the length of the string.
[X] Wrong: "Removing a prefix is always instant, no matter the string size."
[OK] Correct: The shell must check characters one by one to find the pattern, so longer strings take more time.
Understanding how simple string operations scale helps you reason about script performance and avoid surprises in real tasks.
"What if we used the longest match removal syntax (${var##pattern}) instead? How would the time complexity change?"
Practice
${var#pattern} do?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 AQuick Check:
Prefix removal = shortest match = A [OK]
- Confusing # with ## for prefix length
- Mixing prefix removal with suffix removal
- Thinking it removes suffix instead of prefix
abc from variable var in Bash?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 AQuick Check:
Shortest prefix removal = single # = B [OK]
- Using ## instead of # for shortest prefix
- Using % or %% which remove suffixes
- Confusing prefix and suffix removal syntax
var="folder/subfolder/file.txt", what is the output of echo ${var#*/}?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 BQuick Check:
Remove shortest prefix ending with / = subfolder/file.txt [OK]
- Removing longest prefix with ## instead of #
- Expecting only filename instead of subfolder/file.txt
- Confusing prefix removal with suffix removal
var="abc123abc456"
echo ${var##abc*}
It outputs an empty line. Why?Solution
Step 1: Analyze the pattern
The patternabc*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:
Becauseabc*matches the longest prefix, so nothing remains -> Option CQuick Check:
Longest prefix with ## + greedy * = B [OK]
- Confusing ## (longest) with # (shortest)
- Underestimating greedy nature of * in longest match
- Assuming pattern causes error or variable is empty
path="folder/home/user/docs/report.txt". Which command removes only the first slash and everything before it, leaving home/user/docs/report.txt?Solution
Step 1: Understand the variable value
The variablepathis "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 DQuick Check:
Remove shortest prefix up to first slash = D [OK]
- Using ##*/ removes up to last slash (only filename)
- Using #/* or ##/* which don't match without leading slash
- Confusing prefix removal with suffix removal
Ready to prove it?
Pass the test and this pattern moves unknown
