BASH Programming

How to Replace One Character with Another – Bash

In Bash Scripting, it’s common to replace one character with another character in a string as this can be useful for data processing, text manipulation, and many other use cases. Fortunately, Bash provides several built-in methods for doing this and in this article, we’ll cover some of the most common ways to replace characters in a Bash Script.

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:

tr '<character-to-be-replaced>' '<new-character>'

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:

echo "hello Linux" | tr 'L' 'W'

Here is the full bash code that shows how to use the tr command for replacing one character with another:

#!/bin/bash

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:

sed 's/<character-to-be-replaced>/<new-character>/g'

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:

echo "hello Linux" | sed 's/L/w/g'

Here is the full bash code that shows how to use the sed command for replacing one character with another:

#!/bin/bash

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:

#!/bin/bash

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.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.