How to Replace One Character with Another in Bash Script


It’s common practice in Bash scripting to swap out characters in a string because doing so can be applied to a variety of situations, including text manipulation and data processing. We’ll go through some of the most popular techniques for replacing characters in a Bash script in this post because Bash fortunately comes with various built-in methods for achieving this. Here for illustration, I will be using Ubuntu 22.04 operating system

How to Replace One Character with Another in Bash Script

In Bash scripting, changing one character for another is a typical activity that can be helpful in a variety of situations. Here are three quick techniques to change one character in a Bash script for another:

  • Using parameter expression
  • Using sed command
  • Using tr Command

Using Parametric Expression

Bash provides a built-in parameter expansion feature that can be used to replace characters in a string. To replace one character with another using parameter expansion, and here’s an example that replaces “e” of the original string will “x” new character:

#!/bin/bash

orignal_string="Linux genie"

updated_string="${orignal_string//e/x}"

echo "orignal_string:" $orignal_string

echo "updated_string:" $updated_string

In this example, all instances of the letter “e” in the variable orignal_string will be changed to the character “x” using parametric expansion. This command’s result will be “Linux gxnix”:

Using tr Command

The tr command is a straightforward tool for changing, removing, or condensing the order of characters in a string. The syntax for using this command is as follows: It can be used to replace one character with another by specifying the characters to be replaced and their replacements.

tr ‘<replacement-character >’ ‘<replacing-character>’

As an example, if I wanted to change the letter e in the text “Linux genie” to the letter x, I could do it by using the tr command as follows:

echo "Linux genie" | tr 'e' 'x'

The whole bash code that demonstrates how to use the tr command to swap out one character for another is provided below:

#!/bin/bash

orignal_string="Linux genie"

export orignal_string=$(echo "$orignal_string" | tr 'e' 'x')

echo "orignal_string:" $orignal_string

echo "updated_string:" $updated_string

The output for the Bash script is shown below, and e has been changed to x:

Using sed Command

Using the sed command in Bash is another technique to change one character for another. The stream editor sed command can carry out a number of actions on a string, including substitution. The syntax for using this command to swap out one character for another is as follows:

sed ‘s/< replacement-character>/<replacing-character>/g’

As an example, if I wanted to change the letter e in the text “Linux genie” to the letter x, I could do it by using the tr command as follows:

 

echo "Linux genie" | sed 's/e/x/g'

The whole bash code that demonstrates how to use the tr command to swap out one character for another is provided below:

#!/bin/bash

orignal_string="Linux genie"

updated_string=$(echo "$orignal_string" | sed 's/e/x/g')

echo "orignal_string:" $orignal_string

echo "updated_string:" $updated_string

In this illustration, the phrase “Linux genie” is output using the echo command, which is then piped to the sed command. Every time the letter “e” appears, the sed programme substitutes the letter “x” in its place using the substitution (s) command. The g option instructs sed to replace the character throughout the string, not just the initial instance, by specifying it at the end of the command. This command will return “Linux gxnix” as its result.

Conclusion

Replacing one character with another is a common task in bash scripting. In this article, we discussed different methods to replace a single character with another in a bash script. We can use sed, tr, or parameter expansion to replace characters in a string or file. Each method has its own advantages and disadvantages, and we should choose the appropriate method based on our requirements.

Print Friendly, PDF & Email
Categories