0
0
Bash Scriptingscripting~10 mins

Substring extraction (${var:offset:length}) in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to extract the substring 'world' from the variable.

Bash Scripting
text="hello world"
sub=${text:[1]:5}
echo "$sub"
Drag options to blanks, or click blank then click option'
A1
B0
C5
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using offset 0 or 1 extracts the wrong part of the string.
Forgetting that offset counts from zero.
2fill in blank
medium

Complete the code to extract the first 4 characters from the variable.

Bash Scripting
filename="report2024.txt"
prefix=${filename:[1]:4}
echo "$prefix"
Drag options to blanks, or click blank then click option'
A4
B1
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Starting at offset 1 misses the first character.
Using offset equal to length extracts nothing.
3fill in blank
hard

Fix the error in the code to correctly extract '2024' from the filename.

Bash Scripting
filename="report2024.txt"
year=${filename:[1]:4}
echo "$year"
Drag options to blanks, or click blank then click option'
A7
B6
C4
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using offset 7 or 4 extracts wrong parts.
Confusing offset with length.
4fill in blank
hard

Fill both blanks to extract 'script' from the variable.

Bash Scripting
word="bashscript"
sub=${word:[1]:[2]
echo "$sub"
Drag options to blanks, or click blank then click option'
A4
B6
C7
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong offset or length extracts incorrect substring.
Mixing up offset and length values.
5fill in blank
hard

Fill both blanks to extract 'log' from the filename variable.

Bash Scripting
filename="system.log.2024"
sub=${filename:[1]:[2]
echo "$sub"
Drag options to blanks, or click blank then click option'
A7
B3
C4
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using offset 6 or 4 extracts wrong substring.
Using length 4 extracts extra characters.