Replace Characters in a String Using Bash

  1. Method 1 - Use the tr Command
  2. Method 2 - Use Bash Parameter Expansion
  3. Method 3 - Use the sed Command

One common operation involving string literals is replacing individual characters or substrings in that string with other characters or substrings.

In this article, we will look at several methods of string replacements using the BASH shell.

Method 1 - Use the tr Command

The tr is a very handy Bash command that can be used to perform different transformations on a string. It is particularly helpful in string transformations (e.g., changing the case of the string to upper or lower), removing substring repetitions, and searching and replacing substrings.

It is used to replace characters and substrings in a string.


echo "$variableName" | tr [character(s)ToBeRemoved] [character(s)ToBeAdded]

Script Example:

echo "$STR" | tr A Z



The character "A" in the string variable $STR is replaced with the character "Z". It is important to note that the above command will also work for replacing all instances of the character "A".

Script Example:

echo "$STR" | tr A Z



For more complex use cases such as replacing a single character with multiple characters or replacing substrings with other substrings, the tr command can still be used, but it would be necessary to incorporate and use regular expressions(RegEx) to accomplish such tasks.

Another useful command option with tr is the squeeze or -s option, which can replace a sequence of characters or a substring with a single character.

Script Example:

echo "$STR" | tr -s BD Z



As can be seen, multiple adjacent occurrences of the characters “B” and “D” were replaced by a single “Z”.

Method 2 - Use Bash Parameter Expansion

The $ symbol is commonly preceded with a variable name to assign the variable a value or to print its contents. However, we can also use it for Parameter Expansion.

We can use a parameter expansion to replace the contents of a variable with a given substring in the following way.



Please note that the use of braces is optional with the variable expansion.

Script Example:




Note that using a single forward slash after the variable name in the parameter expansion command replaces the first occurrence of the substring only. Use the following script to replace all occurrences of a substring in a string or double forward slashes.

Script Example:


Method 3 - Use the sed Command

The sed command is similar to the tr command. Still, it is more powerful as it can be used to edit text files directly without opening them. sed command in BASH stands for stream editor. It performs many functions on files like searching, finding and replacing, and insertion or deletion.

The sed command can be used for string replacement in the following way.

Script Example:

echo "$STR" | sed -r 's/[Z]+/B/g'



This is similar to using the tr command. The -r option after the sed command indicates replacement, and pattern matching(RegEx) describes what to find and replace it with.

Note that the syntax for the sed command varies slightly between bash versions on different systems, which may cause unforeseen behavior.

Related Article - Bash String

  • Convert String to Integer in Bash
  • String Comparison Operator in Bash
  • Remove Last Character From a String in Bash