Challenge - 5 Problems
Substring Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this substring extraction?
Given the variable assignment
text="automation", what is the output of echo ${text:4:3}?Bash Scripting
text="automation" echo ${text:4:3}
Attempts:
2 left
💡 Hint
Remember that offset starts at 0 and length is how many characters to extract.
✗ Incorrect
The string 'automation' indexed from 0 is: a(0) u(1) t(2) o(3) m(4) a(5) t(6) i(7) o(8) n(9). Starting at offset 4, length 3 extracts 'm', 'a', 't'.
💻 Command Output
intermediate2:00remaining
What happens if length exceeds string length?
Given
word="script", what is the output of echo ${word:3:10}?Bash Scripting
word="script" echo ${word:3:10}
Attempts:
2 left
💡 Hint
If length goes beyond the string end, bash extracts until the end.
✗ Incorrect
Starting at offset 3 ('r'(0), 'i'(1), 'p'(2), 't'(3)), length 10 tries to extract beyond string end, but bash returns till end: 'ipt'.
📝 Syntax
advanced2:00remaining
Which option correctly extracts substring with negative offset?
Given
data="automation", which command outputs the last 4 characters using substring extraction?Bash Scripting
data="automation"Attempts:
2 left
💡 Hint
Negative offset counts from the end; length can be omitted to go to string end.
✗ Incorrect
Option C uses negative offset -4 and no length, extracting last 4 chars 'tion'. Option C has a space causing syntax error. Option C uses wrong syntax. Option C has negative length which is invalid.
🔧 Debug
advanced2:00remaining
Why does this substring extraction fail?
Given
str="automation", why does echo ${str:3,4} produce an error?Bash Scripting
str="automation" echo ${str:3,4}
Attempts:
2 left
💡 Hint
Check the syntax for substring extraction carefully.
✗ Incorrect
Substring extraction syntax uses colons, not commas. Using a comma causes a syntax error.
🚀 Application
expert3:00remaining
Extract middle word from a string using substring extraction
Given
sentence="learn bash scripting easily", which command extracts the word 'bash' using substring extraction only (no external commands)?Bash Scripting
sentence="learn bash scripting easily"Attempts:
2 left
💡 Hint
Count characters including spaces to find offset and length of 'bash'.
✗ Incorrect
The word 'bash' starts at offset 6 (counting from 0) and has length 4. So ${sentence:6:4} extracts 'bash'.