Challenge - 5 Problems
String Replacement 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 string replacement?
Given the following bash script, what will be printed?
Bash Scripting
text="hello world"
echo ${text/world/universe}Attempts:
2 left
💡 Hint
The syntax ${var/old/new} replaces the first occurrence of 'old' with 'new' in the variable's value.
✗ Incorrect
The variable 'text' contains 'hello world'. The expression ${text/world/universe} replaces the first 'world' with 'universe', so the output is 'hello universe'.
💻 Command Output
intermediate2:00remaining
What does this double slash replacement output?
What will this bash command output?
text="banana bandana"
echo ${text//ana/123}
Bash Scripting
text="banana bandana" echo ${text//ana/123}
Attempts:
2 left
💡 Hint
Using double slashes replaces all occurrences, not just the first.
✗ Incorrect
The expression ${text//ana/123} replaces all 'ana' substrings with '123'. So 'banana' becomes 'b123na' and 'bandana' becomes 'band123'.
💻 Command Output
advanced2:00remaining
What is the output when replacing a substring that does not exist?
Consider this bash code:
text="apple orange"
echo ${text/applee/fruit}
Bash Scripting
text="apple orange"
echo ${text/applee/fruit}Attempts:
2 left
💡 Hint
If the substring to replace is not found, the original string remains unchanged.
✗ Incorrect
Since 'applee' is not found in 'apple orange', no replacement occurs, so the output is the original string.
💻 Command Output
advanced2:00remaining
What is the output of this nested replacement?
What will this bash script print?
text="foo bar foo"
echo ${text/foo/bar}
echo ${text//foo/bar}
Bash Scripting
text="foo bar foo"
echo ${text/foo/bar}
echo ${text//foo/bar}Attempts:
2 left
💡 Hint
Single slash replaces first occurrence; double slash replaces all occurrences.
✗ Incorrect
The first echo replaces only the first 'foo' with 'bar', resulting in 'bar bar foo'. The second echo replaces all 'foo' with 'bar', resulting in 'bar bar bar'.
💻 Command Output
expert2:00remaining
What is the output when using replacement with special characters?
Given this bash code, what will be the output?
text="path/to/file.txt"
echo ${text//./_}
Bash Scripting
text="path/to/file.txt"
echo ${text//./_}Attempts:
2 left
💡 Hint
The dot '.' is replaced by underscore '_' everywhere in the string.
✗ Incorrect
The expression replaces all '.' characters with '_'. Since there is only one dot before 'txt', it becomes 'path/to/file_txt'.