Substring extraction (${var:offset:length}) in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to extract a substring changes as the size of the substring or the original string changes.
How does the work grow when we ask for longer parts of the string?
Analyze the time complexity of the following code snippet.
string="Hello, world!"
sub1=${string:0:5}
sub2=${string:7:5}
sub3=${string:0:10}
# Extract different parts of the string
This code extracts parts of a string using the substring syntax: starting at an offset and taking a certain length.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Copying characters from the original string to create the substring.
- How many times: Once for each character in the substring length.
Extracting a substring means copying each character one by one for the requested length.
| Input Size (substring length) | Approx. Operations (character copies) |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The work grows directly with the length of the substring requested.
Time Complexity: O(k)
This means the time to extract a substring grows linearly with the length of the substring, not the whole string.
[X] Wrong: "Extracting a substring always takes time proportional to the whole string length."
[OK] Correct: The operation only copies the requested part, so time depends on substring length, not the full string size.
Knowing how substring extraction scales helps you write efficient scripts and answer questions about string handling in interviews confidently.
"What if we extract multiple substrings in a loop from a very long string? How would the time complexity change?"
Practice
${var:2:3} do in bash scripting?Solution
Step 1: Understand substring syntax
The syntax${var:offset:length}extracts a substring fromvarstarting atoffsetwithlengthcharacters.Step 2: Apply to given expression
Here, offset is 2 and length is 3, so it extracts 3 characters starting from index 2 (0-based).Final Answer:
Extracts 3 characters fromvarstarting at index 2 -> Option AQuick Check:
Substring = characters from index 2, length 3 [OK]
- Confusing offset with length
- Forgetting offset starts at 0
- Assuming length is mandatory
text?Solution
Step 1: Recall correct substring syntax
The correct syntax uses colon separators:${var:offset:length}.Step 2: Match syntax to options
${text:1:4} matches this exactly with offset 1 and length 4. Others use invalid separators or wrong order.Final Answer:
${text:1:4} -> Option AQuick Check:
Correct syntax uses colons [OK]
- Using commas or dashes instead of colons
- Swapping offset and length
- Missing braces around variable
filename="report2024.txt", what is the output of echo ${filename:6:4}?Solution
Step 1: Identify substring start and length
Offset is 6, length is 4. The string is "report2024.txt".Step 2: Extract substring from position 6
Counting from 0: r(0) e(1) p(2) o(3) r(4) t(5) 2(6) 0(7) 2(8) 4(9). Extract 4 chars starting at index 6: "2024".Final Answer:
2024 -> Option BQuick Check:
Substring at 6 length 4 = "2024" [OK]
- Starting count at 1 instead of 0
- Extracting wrong substring length
- Confusing characters with digits
var="hello world"
echo ${var:5,3}Solution
Step 1: Check substring syntax
The substring syntax requires colons between offset and length, not commas.Step 2: Identify error in code
The code uses a comma${var:5,3}which is invalid and causes a syntax error.Final Answer:
Comma is invalid separator; should use colon -> Option CQuick Check:
Use colons, not commas [OK]
- Using commas instead of colons
- Assuming offset out of range causes error
- Misnaming variable
date="2024-06-15". Which command extracts the month part ("06") using substring extraction?Solution
Step 1: Locate month position in date string
The string is "2024-06-15". Counting from 0: 2(0) 0(1) 2(2) 4(3) -(4) 0(5) 6(6) -(7) 1(8) 5(9). Month starts at index 5.Step 2: Extract 2 characters starting at index 5
Using${date:5:2}extracts "06" which is the month.Final Answer:
${date:5:2} -> Option DQuick Check:
Month = substring at 5 length 2 [OK]
- Starting count at 1 instead of 0
- Extracting wrong substring length
- Confusing day and month positions
Ready to prove it?
Pass the test and this pattern moves unknown
