Replacing String in Bash

Bash is a command line interpreter that executes the user commands to modify the text in Linux distribution. Sometimes you need to change a specific string/pattern with another string while writing the bash script but don’t know how to do it. This guide will cover all the possible methods to replace strings in the bash script. The content of this post is as follows:

Let’s replace performing the different examples.

How to Replace a String in Bash?

In this section, we will replace a word/pattern at the first occurrence, all occurrences in a string, and all the instances in a specific file using a bash script. Let’s perform these actions using the below-mentioned methods:

  • Using the sed Command
  • Using the Native Substring {EXPRESSION}

Let’s head over to the sed command method.

Method 1: Using sed Command

How to install Kali Linux in Window...

The sed abbreviation of the “Stream Editor” command is utilized in Linux to help in functions like replacing/modifying the text, selecting/deleting the text, and many others. Let’s use this command to replace the string in the bash script.

The syntax of the “sed” command is provided below:

$ sed -i "s/old-string/new-string"             #For replacing a word in a line.
$ sed -i "s/old-string/new-string/g"           #For replacing a word at all instances in a line.
$ sed -i 's/old_string/new_string/' filename   #For replacing a word at all occurrences in a file.

The sed command entities are described below:

  • i: Option used to modify the content of a file.
  • s: Represents the substitute command.
  • g: Performs the function at all instances in a line.
  • old_string: Replace this with the word/pattern you want to modify.
  • new_string: The new word/pattern.
  • Filename: Put the name of the bash file.

Let’s practice it through examples.

Example 1: Replace First Occurrence Word in a String

We can replace a single word or several words in a string/file using the “s” option within the “sed” command. In this example, the bash script will replace the first occurrence permanently with the sed command in a string variable “Welcome to bash script.” for the word “bash” with the new word “shell”.

Bash Script:

#!/bin/bash
string="Welcome to bash script."
echo $string
string=$(sed "s/bash/shell/" <<< "$string")
echo $string

The bash script code will be written in a bash file named “testscript.sh”, that shell file will be used throughout this guide with changing the desired script (code). The above script code file is shown below:

After creating the bash script file “testscript.sh”, run the file in the terminal as follows:

$ bash testscript.sh

The output shows the change of the “bash” word with the “shell” in the bash string.

Example 2: Replace at All Occurrences in a String

The sed command helps us to replace all the instances in a line. The option “g” replaces the matching words in the bash script at all occurrences. For instance, the below script string variable has a “bash” word at three occurrences that will be replaced by a “shell” word using single sed command in a bash script:

Bash Script:

#!/bin/bash
string="Welcome to bash script.
This is a new bash script line.
This is another new bash script line."
echo $string
string=$(sed "s/bash/shell/g" <<< "$string")
echo $string

Let’s execute the script:

$ bash testscript.sh

As highlighted in the screenshot, all the occurrences of “bash” are replaced with the “shell”.

Example 3: Replace at All Occurrences in a File

We can replace a string/pattern at all the instances in a file using the filename with the sed command. The below command shows that all the occurrences where the “bash” word is used will be replaced with the “shell” word in a file named “testFile.txt”:

Bash Script:

#!/bin/bash sed -i 's/bash/shell/g' testFile.txt

Let’s first check the “testfFle.txt” content using the “cat” command which has bash word at three instances. After that, run the bash script that will replace all the occurrences of the “bash” word with “shell”. To verify the “testFil.txt” content is changed, execute the cat command:

$ cat testFile.txt             #To print the testfile.txt content before changing.
$ bash script.sh               #Run bash script to make changes.
$ cat testFile.txt             #To print the testfile.txt content after changing.

The output clearly shows the replacement of the “bash” word in the content with the “shell” in three instances.

Method 2: Using the Native Substring {EXPRESSION}

The easiest way to replace a word/pattern from a string is using the {EXPRESSION}command. The syntax for this method is mentioned below:

$ {old-string/search-pattern/new-pattern}

The syntax components are given below:

  • old-string: It indicates the string in which the word will be replaced.
  • search-pattern: Replace it with the word/pattern that will be replaced.
  • new-string: It will be replaced with the new word/pattern for replacing the search pattern.

Let’s start replacing strings in the bash script with the help of examples.

Example 1: Replace a String Word in Bash

We can replace a specific word in the string while writing a bash/shell script using the {Expression} syntax. Let’s understand the below bash script code where the word “bash” will be replaced with the “shell” in a string variable “Welcome to bash script. ”.

Bash Script:

#!/bin/bash
string="Welcome to bash script."
echo $string
string="${string/bash/shell}"
echo $string

Let’s execute it:

$ bash testscript.sh

The output shows the word “bash” is successfully replaced with “shell” in the script file.

Example 2: Replace a String Variable With Another in Bash

A variable stores a string or word that helps us to use it anywhere in the code without writing it again and again. For instance, in the below script, a variable named “string” is declared with the text “Welcome to bash script.” in which the replace variable will be replaced with the replace with variable, using the below bash/shell script:

Bash Script:

#!/bin/bash
string="Welcome to bash script."
echo $string
replace="bash"
replacewith="shell"
string="${string/${replace}/${replacewith}}"
$ echo $string

Execute the bash script:

$ bash testscript.sh

The output clearly shows the replacement of “bash” with the “shell” word in a string.

Example 3: Replace a Word at All Occurrences in Bash

If you want to replace a word at all instances, not only in a specific string. The use of “//” before the search pattern helps us to replace all the occurrences with the new search word/pattern. The below script will replace the “bash” word with the “shell” word at all instances:

Bash Script:

#!/bin/bash
string="Welcome to bash script.
A new bash script line."
$ echo $string
string="${string//bash/shell}"
$ echo $string

Executing a Bash Script as follows:

$ bash testscript.sh

The bash word was used twice before, which was replaced with the “shell” word after running the bash file.

Conclusion

Two methods are used to replace strings in bash/shell scripts that use the “sed” command or the “native substring {EXPRESSION}” in Linux. These methods can replace a single word in the line, a variable with another variable, all occurrences in a line word, and all instances in a file. This post has briefly explained both methods to replace a string in Bash.