Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the expression ${var:2:3} do in bash scripting?
easy
A. Extracts 3 characters from var starting at index 2
B. Extracts 2 characters from var starting at index 3
C. Extracts 3 characters from var starting at index 0
D. Extracts the entire string var ignoring offset and length

Solution

  1. Step 1: Understand substring syntax

    The syntax ${var:offset:length} extracts a substring from var starting at offset with length characters.
  2. 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).
  3. Final Answer:

    Extracts 3 characters from var starting at index 2 -> Option A
  4. Quick Check:

    Substring = characters from index 2, length 3 [OK]
Hint: Offset is start index, length is number of chars [OK]
Common Mistakes:
  • Confusing offset with length
  • Forgetting offset starts at 0
  • Assuming length is mandatory
2. Which of the following is the correct syntax to extract 4 characters starting at position 1 from variable text?
easy
A. ${text:1:4}
B. ${text:4:1}
C. ${text:1-4}
D. ${text:1,4}

Solution

  1. Step 1: Recall correct substring syntax

    The correct syntax uses colon separators: ${var:offset:length}.
  2. 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.
  3. Final Answer:

    ${text:1:4} -> Option A
  4. Quick Check:

    Correct syntax uses colons [OK]
Hint: Use colons, not commas or dashes [OK]
Common Mistakes:
  • Using commas or dashes instead of colons
  • Swapping offset and length
  • Missing braces around variable
3. Given filename="report2024.txt", what is the output of echo ${filename:6:4}?
medium
A. port
B. 2024
C. 202
D. txt

Solution

  1. Step 1: Identify substring start and length

    Offset is 6, length is 4. The string is "report2024.txt".
  2. 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".
  3. Final Answer:

    2024 -> Option B
  4. Quick Check:

    Substring at 6 length 4 = "2024" [OK]
Hint: Count from zero, then take length chars [OK]
Common Mistakes:
  • Starting count at 1 instead of 0
  • Extracting wrong substring length
  • Confusing characters with digits
4. What is wrong with this code snippet?
var="hello world"
echo ${var:5,3}
medium
A. Offset 5 is out of range
B. Length 3 is too large
C. Comma is invalid separator; should use colon
D. Variable name is incorrect

Solution

  1. Step 1: Check substring syntax

    The substring syntax requires colons between offset and length, not commas.
  2. Step 2: Identify error in code

    The code uses a comma ${var:5,3} which is invalid and causes a syntax error.
  3. Final Answer:

    Comma is invalid separator; should use colon -> Option C
  4. Quick Check:

    Use colons, not commas [OK]
Hint: Use colons between offset and length [OK]
Common Mistakes:
  • Using commas instead of colons
  • Assuming offset out of range causes error
  • Misnaming variable
5. You have a date string date="2024-06-15". Which command extracts the month part ("06") using substring extraction?
hard
A. ${date:3:2}
B. ${date:6:2}
C. ${date:4:2}
D. ${date:5:2}

Solution

  1. 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.
  2. Step 2: Extract 2 characters starting at index 5

    Using ${date:5:2} extracts "06" which is the month.
  3. Final Answer:

    ${date:5:2} -> Option D
  4. Quick Check:

    Month = substring at 5 length 2 [OK]
Hint: Count chars from zero, dash counts as char [OK]
Common Mistakes:
  • Starting count at 1 instead of 0
  • Extracting wrong substring length
  • Confusing day and month positions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard