0
0
Bash Scriptingscripting~20 mins

String replacement (${var/old/new}) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Replacement 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 string replacement?
Given the following bash script, what will be printed?
Bash Scripting
text="hello world"
echo ${text/world/universe}
Ahello universe
Bhello world
Cuniverse world
Dhello
Attempts:
2 left
💡 Hint
The syntax ${var/old/new} replaces the first occurrence of 'old' with 'new' in the variable's value.
💻 Command Output
intermediate
2: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}
Ab123 b123d123
Bb123na band123
Cbanana bandana
Db123na b123d123
Attempts:
2 left
💡 Hint
Using double slashes replaces all occurrences, not just the first.
💻 Command Output
advanced
2: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}
ASyntax error
Bfruit orange
Corange
Dapple orange
Attempts:
2 left
💡 Hint
If the substring to replace is not found, the original string remains unchanged.
💻 Command Output
advanced
2: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}
A
bar bar foo
bar bar bar
B
bar bar foo
foo bar foo
C
foo bar foo
bar bar bar
D
bar bar foo
bar foo bar
Attempts:
2 left
💡 Hint
Single slash replaces first occurrence; double slash replaces all occurrences.
💻 Command Output
expert
2: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//./_}
Atxt_elif/ot/htap
Bpath/to/file_txt_
Cpath/to/file_txt
Dath/to/file_txt
Attempts:
2 left
💡 Hint
The dot '.' is replaced by underscore '_' everywhere in the string.