0
0
Bash Scriptingscripting~20 mins

Substring extraction (${var:offset:length}) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Substring Extraction Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2: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}
Amat
Buto
Ctom
Doma
Attempts:
2 left
💡 Hint
Remember that offset starts at 0 and length is how many characters to extract.
💻 Command Output
intermediate
2: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}
Apt
Bipt
Cip
DError: length too long
Attempts:
2 left
💡 Hint
If length goes beyond the string end, bash extracts until the end.
📝 Syntax
advanced
2: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"
Aecho ${data:-4:4}
Becho ${data: -4:4}
Cecho ${data: -4}
Decho ${data: -4:-1}
Attempts:
2 left
💡 Hint
Negative offset counts from the end; length can be omitted to go to string end.
🔧 Debug
advanced
2: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}
AComma is invalid syntax; should use colon between offset and length
BOffset 3 is out of range for this string
CLength 4 is too large and causes error
DVariable name must be uppercase
Attempts:
2 left
💡 Hint
Check the syntax for substring extraction carefully.
🚀 Application
expert
3: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"
Aecho ${sentence:6:4}
Becho ${sentence:6:5}
Cecho ${sentence:6:3}
Decho ${sentence:7:4}
Attempts:
2 left
💡 Hint
Count characters including spaces to find offset and length of 'bash'.