Challenge - 5 Problems
String Manipulation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why is string manipulation common in bash scripts?
In bash scripting, why do we often manipulate strings like filenames, paths, or command outputs?
Attempts:
2 left
💡 Hint
Think about what kind of data bash scripts usually process.
✗ Incorrect
Bash scripts often work with text like filenames, outputs of commands, or user input. Manipulating strings helps automate tasks like extracting parts of filenames or formatting outputs.
💻 Command Output
intermediate2:00remaining
Output of string slicing in bash
What is the output of this bash command?
filename="report_2024.txt"
echo ${filename:7:4}
Bash Scripting
filename="report_2024.txt" echo ${filename:7:4}
Attempts:
2 left
💡 Hint
The syntax ${variable:start:length} extracts a substring.
✗ Incorrect
The substring starts at index 7 (0-based) and takes 4 characters: '2024'.
📝 Syntax
advanced2:00remaining
Correct syntax for replacing substring in bash
Which option correctly replaces the first occurrence of 'cat' with 'dog' in the variable text?
Bash Scripting
text="the cat sat on the cat"Attempts:
2 left
💡 Hint
Use bash parameter expansion for substring replacement.
✗ Incorrect
Option A replaces the first 'cat' with 'dog'. Option A replaces all occurrences. Options A and D are invalid syntax.
🔧 Debug
advanced2:00remaining
Identify the error in string trimming
What is the output of this bash command?
path="/home/user/docs/file.txt"
echo ${path#*/home/}
Bash Scripting
path="/home/user/docs/file.txt" echo ${path#*/home/}
Attempts:
2 left
💡 Hint
The # operator removes the shortest match from the start.
✗ Incorrect
The pattern '*/home/' matches '/home/' at the start, so it removes '/home/' and everything before it, leaving 'user/docs/file.txt'.
🚀 Application
expert3:00remaining
Extract domain from email using bash string manipulation
Given the variable email="user@example.com", which command extracts only the domain part (example.com)?
Bash Scripting
email="user@example.com"Attempts:
2 left
💡 Hint
Use parameter expansion to remove everything before '@'.
✗ Incorrect
Option B removes the shortest match up to '@', leaving 'example.com'. Option B removes the longest match, which is the same here but less commonly used. Options B and C remove the domain, not extract it.