String Replacement
In this page:
String Replacement
${variable/pattern/replacement} replaces the first match of pattern in the string, while ${variable//pattern/replacement} replaces every match. This is a built-in alternative to piping through sed for simple substitutions. Patterns support basic globbing, not full regular expressions.
Note: Use the double-slash form, ${var//old/new}, when you need to replace all occurrences, not just the first.
Example: String Replacement
#!/bin/bash
text="foo bar foo baz"
echo "${text/foo/qux}"
echo "${text//foo/qux}"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: