0
0
Bash Scriptingscripting~10 mins

Substring extraction (${var:offset:length}) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Substring extraction (${var:offset:length})
Start with variable
Apply substring syntax
Extract substring from offset
Limit length to given number
Output substring
This flow shows how bash extracts a substring from a variable starting at a given offset and for a specified length.
Execution Sample
Bash Scripting
text="Hello World"
sub=${text:6:5}
echo "$sub"
Extracts 5 characters from 'text' starting at position 6 (0-based), printing 'World'.
Execution Table
StepVariable 'text'OperationOffsetLengthSubstring ExtractedOutput
1"Hello World"Set variable----
2"Hello World"Extract substring65"World"-
3"Hello World"Print substring65"World"World
4-End----
💡 Extraction complete and substring printed.
Variable Tracker
VariableStartAfter ExtractionFinal
text"Hello World""Hello World""Hello World"
subundefined"World""World"
Key Moments - 3 Insights
Why does the offset start at 0 and not 1?
In bash substring extraction, counting starts at 0, so offset 6 means the 7th character. See execution_table step 2 where offset 6 extracts 'World'.
What happens if length is longer than the remaining string?
Bash extracts until the end without error. For example, if length exceeds string end, it just returns the rest. This is shown implicitly as length 5 from offset 6 extracts 5 chars safely.
Can offset be negative?
Yes, negative offsets are supported in this syntax. They count from the end of the string. For example, ${text: -5:5} extracts 'World'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the substring extracted at step 2?
A"World"
B"Hello"
C"Hello World"
D"orld"
💡 Hint
Check the 'Substring Extracted' column at step 2 in execution_table.
At which step is the substring printed to output?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where 'Output' column shows the substring in execution_table.
If we change offset to 0 and length to 5, what substring would be extracted?
A"Hello World"
B"World"
C"Hello"
D"ello "
💡 Hint
Think about starting at the first character (offset 0) and taking 5 characters.
Concept Snapshot
Substring extraction syntax: ${var:offset:length}
- 'var' is your string variable
- 'offset' is where to start (0-based)
- 'length' is how many characters to take
- Extracts substring safely even if length exceeds string end
- Negative offsets supported (count from end)
Full Transcript
This visual trace shows how bash substring extraction works using the syntax ${var:offset:length}. We start with a variable 'text' set to 'Hello World'. At step 2, we extract 5 characters starting from offset 6 (the 7th character), which results in 'World'. Then at step 3, this substring is printed. The offset counts from zero, so offset 6 points to the 'W' in 'World'. If length is longer than the remaining string, bash extracts until the end without error. Negative offsets are supported and count from the end of the string. This step-by-step helps beginners see how variables change and how the substring is extracted and output.