How to Replace One Character with Another in Bash Script
Replacing one character with another is a common task in Sash Scripting and can be useful in many different scenarios. Here are the three easy ways to replace one character with another in a Bash Script:
How to Replace One Character with Another Using tr Command in Bash
The tr command is a simple tool for translating, deleting, or squeezing characters in a string. It can be used to replace one character with another by specifying the characters to be replaced and their replacements, here is the syntax for using this command:
Here for illustration if I have taken a string that is “Hello Linux” and if I want to replace the uppercase letter L with uppercase letter W, it can be done with using the tr command like this:
Here is the full bash code that shows how to use the tr command for replacing one character with another:
Main_string="hello Linux"
export New_string=$(echo "$Main_string" | tr 'L' 'w')
echo "Main_string:" $Main_string
echo "Modified_string:" $New_string
Here is the output for the Bash Script, L is replaced with w:
How to Replace One Character with Another Using sed Command in Bash
Another way to replace a character with another character in Bash is to use the sed command. The sed command is a stream editor that can perform various operations on a string, including substitution. Here is the syntax to use this command for replacing one character with another:
Here for illustration if I have taken a string that is “Hello Linux” and I want to replace the lowercase letter “L” with “w” then it can be done with using the tr command like this:
Here is the full bash code that shows how to use the sed command for replacing one character with another:
Main_string="hello Linux"
New_string=$(echo "$Main_string" | sed 's/L/w/g')
echo "Main_string:" $Main_string
echo "Modified_string:" $New_string
In this example, we’re using the echo command to output the string “hello Linux “, and then piping the output to the sed command. The sed command is using the substitution (s) command to replace all occurrences of the letter “L” with the letter “w”. The g option at the end of the command tells sed to replace all occurrences of the character in the string, not just the first occurrence. The output of this command will be “hello winux”:
How to Replace One Character with Another Using Parameter Expansion in Bash
Another way to replace a character with another in bash, you can be by using a parameter expansion and here’s an example that replaces “L” of the original string will “w” new character:
Main_string="hello Linux"
New_string="${Main_string//L/w}"
echo "Main_string:" $Main_string
echo "Modified_string:" $New_string
In this example, we’re using parameter expansion to replace all occurrences of the lowercase letter “h” with the letter “s” in the Main_string variable. The output of this command will be “hello winux”:
Conclusion
There are several ways to replace a character with another character in Bash, including using the tr command, the sed command, and the parameter expansion. Each method has its own advantages and disadvantages, so choose the one that works best for your specific use case as this guide gives all the three methods to do it.