Bash Script to Replace Character in String Easily
Use Bash parameter expansion like
new_string=${old_string//old_char/new_char} to replace all occurrences of a character in a string.Examples
Inputhello world, replace o with a
Outputhella warld
Inputbanana, replace a with o
Outputbonono
Inputno change here, replace x with y
Outputno change here
How to Think About It
To replace a character in a string in Bash, think of the string as text where you want to swap one character for another everywhere it appears. Bash lets you do this easily by using a special syntax that replaces all instances of the old character with the new one without needing external tools.
Algorithm
1
Get the original string input.2
Identify the character to replace and the new character.3
Use Bash parameter expansion to replace all occurrences of the old character with the new character.4
Store the result in a new variable.5
Print the new string.Code
bash
#!/bin/bash old_string="banana" old_char="a" new_char="o" new_string=${old_string//${old_char}/${new_char}} echo "$new_string"
Output
bonono
Dry Run
Let's trace replacing 'a' with 'o' in 'banana' through the code
1
Set original string
old_string="banana"
2
Set characters to replace
old_char="a", new_char="o"
3
Replace characters
new_string = "banana" with all 'a' replaced by 'o' → "bonono"
4
Print result
Output: bonono
| Step | Variable | Value |
|---|---|---|
| 1 | old_string | banana |
| 2 | old_char | a |
| 2 | new_char | o |
| 3 | new_string | bonono |
| 4 | output | bonono |
Why This Works
Step 1: Parameter Expansion
Bash uses ${variable//search/replace} to replace all occurrences of search with replace in variable.
Step 2: Double Slashes Meaning
The double slashes // mean replace all matches, not just the first one.
Step 3: No External Commands
This method works inside Bash without calling external tools like sed or tr, making it fast and simple.
Alternative Approaches
Using sed command
bash
old_string="banana" echo "$old_string" | sed 's/a/o/g'
Uses external tool sed; good for complex patterns but slower than parameter expansion.
Using tr command
bash
old_string="banana" echo "$old_string" | tr 'a' 'o'
Simple character replacement; cannot replace strings longer than one character.
Complexity: O(n) time, O(n) space
Time Complexity
The replacement scans the entire string once, so time grows linearly with string length.
Space Complexity
A new string is created to hold the result, so space also grows linearly with string length.
Which Approach is Fastest?
Bash parameter expansion is fastest as it avoids external calls; sed and tr are slower due to process overhead.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bash parameter expansion | O(n) | O(n) | Simple character/string replacement inside scripts |
| sed command | O(n) | O(n) | Complex pattern replacements, regex support |
| tr command | O(n) | O(n) | Single character replacements only |
Use Bash parameter expansion
${var//old/new} for quick and efficient character replacement.Forgetting to use double slashes
// replaces only the first occurrence, not all.