How to Replace String in Bash? [5 Methods]

In bash scripting, the replacement of string is important because it allows you to modify a variable and the text of a file. It also helps in file and text processing and to validate user input.

To replace a string in bash, check the following methods:

  1. Using Parameter expansion: ${String/pattern/replacement}
  2. Using the sed command: sed 's/pattern/replacement/' filename
  3. Using the awk command: "input_str" | awk 'pattern { action }'
  4. Using the perl command: perl [options] -e 'action' filename
  5. Using the tr command: tr 'old_chars' 'new_chars' < "input_file"

Dive into the article to learn these methods of how to replace a bash string in detail.

1. Using Parameter Expansion

Using the parameter expansion you can simply replace a string without using any external command. Parameter expansion refers to retrieving and manipulating the value that is stored in the parameter or variable. By using the//with the${string}syntax you can replace string. The “//” denotes the parameter substitution within the parameter expansion.

To replace a character in a bash string using parameter expansion, check out the script below:

#!/bin/bash

# This script replaces the space with an underscore in the input string
input="hello world"

# Replace space with underscore using parameter expansion
modified_string="${input// /_}"

# Print the modified string
echo "$modified_string"
EXPLANATION

With the modified_string="${input// /}", the script replaces the space of the input string with an underscore (_). Lastly, the echo command prints the output at the terminal.

Replace a space with underscore in bash

The output shows the script has replaced the space of the string with “_”.

Note: Using parameter expansion you can not replace a string in a file directly. So it is better to use an external command like sed, or awk to replace the string.

Replace First Occurrences in Bash String

To replace the first occurrence of a string using the parameter expansion, you can use the following script:

#!/bin/bash

newstr="This is the old_string. This old_string will be changed."

# Replace all occurrences of "old_string" with "replaced_string"
newstr="${newstr/old_string/replaced_string}"

echo "Original string: This is the old_string. This old_string will be changed."
echo "Updated string: $newstr"
EXPLANATION

At first, with ${newstr}, the script accesses the input string. Then it specifies the substitution pattern/old_string/replace_stringwhich replaces the string old_string with replaced-string.

Replace the first occurrence of a string in bash using parameter expansion

The output shows the script replaced only the first occurrences of “old_string” with “replaced_string”.

Replace Multiple Occurrences

To replace multiple occurrences with parameter expansion, you have to use “//” instead of “/” before the matched string or after the “original_string”. Take a look at the script below:

#!/bin/bash

# Set nocasematch option to make string comparisons case-insensitive
shopt -s nocasematch

original_string="This is a sample sentence with some matches. Matches are repeated. More matches."

# Replace all occurrences of "matches" with "replacements"
updated_string="${original_string//matches/replacements}"

# Set nocasematch option to make string comparisons case-insensitive
shopt -s nocasematch

echo "Original string: $original_string"
echo "Updated string: $updated_string"
EXPLANATION

At first, the bash script uses shopt -s nocasematch to control case sensitivity. Then the script accesses the input string using ${original_string}. After that, it uses//to ensure the substitution of all occurrences of the input string. Later with matches/replacements, the script replaces the string “matches” with the string “replacements”.

Replace multiple occurrences using parameter expansionThe output shows the script has replaced all the occurrences of “matches” with “replacements”.

Replace Shortest Match from Front and End Of a String

To replace the first front-end and back-end match, you can use the#and%respectively before the string to replace. Take a look at the script below:

#!/bin/bash

string="abcXYZabc"
string_to_replace="abc"
replacing_string="AbcRstNopAbcVwx"

echo "**Example of replacing the first front end matched string: $string_to_replace With: $replacing_string**"
echo "Output: ${string/#$string_to_replace/$replacing_string}"

echo "**Example of replacing the first back end matched string: $string_to_replace With: $replacing_string**"
echo "Output: ${string/%$string_to_replace/$replacing_string}"
EXPLANATION

At first, the script declares a string in a variable namedstring. After that, it declares a string that needs to be replaced and a replacing string. Then the${string/#}targets the first match occurrence of$string_to_replaceat the beginning and replaces it with replacing_string. Similarly, the${string/%}syntax, targets the first match occurrences of string_to_replace at the end and replaces it with replacing_string.

Replace a string from the front end and the back end

The output shows the script has replaced the front end of the string abc with AbcRstNopAbcVwx and also replaced the back end of the string abc with AbcRstNopAbcVwx.

2. Using the “sed” Command

The sed command is a simple yet very useful command for stream editors in bash scripting. The most common application of the sed command is the string replacement. It facilitates the replacement and manipulation of strings in files. Using the “sed” command, you can search and replace complex bash strings in a file directly, match patterns, and replace multiple occurrences of a string. To replace a string using the sed command, let’s take a look at the following section:

A. Replace the First Character

To replace the first character of a string using thesedcommand, check the following script:

#!/bin/bash

# This script replaces the first character of a string with a new character
input="example"

# Replace the first character with 'X'
modified_string=$(echo "$input" | sed 's/./X/')

# Print the modified string
echo "$modified_string"
EXPLANATION

At first, the script declares a string in a variable namedinput. Then, the script replaces the first character withmodified_string=$(echo "$input" | sed 's/./X/')syntax. Here the pipe (|) operator takes the output of the echo command to the sed command. With /./, the sed command matches the first character and replaces it with X. Later, the echo command prints the modified string in the terminal.

Replace the first character of a string using sed command

The script has successfully replaced the first character of the input string “e” with another character “X”.

Note: To replace special characters with another string or special character if you use the following code then it won’t show the expected result.

#!/bin/bash

echo 'Hello, World.' | sed 's/./!/'

#Output: !ello, World.

Here, the expected result was Hello, World!. But the script changed the first character of the string.

To replace special characters use\immediate before that character so that you can escape the special character. Take a look at the script:

#!/bin/bash

echo 'Hello, World.' | sed 's/\./!/'

#Output: Hello, World!

B. Replace Specific Characters

To replace a specific character of a string using the sed command, follow the code below:

#!/bin/bash

# This script replaces specific characters in a string using sed
input="Hello, World!"

# Replace 'H' with 'X' and 'o' with '!'
modified_string=$(echo "$input" | sed 's/H/X/g; s/o/!/g')

# Print the modified string
echo "$modified_string"
EXPLANATION

In the $(echo "$input" | sed 's/H/X/g; s/o/!/g'), thepipeoperator takes the output of theechocommand to thesedcommand. Here thes/H/X/g; s/o/!/greplaces all occurrences of H with X and o with ! because of the -g flag.

Replace the specific character using the sed command

The output shows that the script has replaced all the occurrences of the characters ‘H’ and ‘o’ with ‘X’ and  ‘!’ respectively.

C. Replace First Occurrences

To replace the first occurrences or a single string you can use the substitution -s option within the sed command. Check the following script:

#!/bin/bash

# Initial string
str="New string, another New string"

# Old string to be replaced
oldstr="New"

# New string to replace the old string
newstr="Replaced"

# Using sed to replace occurrences of oldstr with newstr in str
result=$(echo "$str" | sed "s/$oldstr/$newstr/")

# Displaying the original and replaced strings
echo "Original String :  $str"
echo "Replaced String :  $result"
EXPLANATION

At first, the script declares a string in a variable named str. Then the script also declares the old and new strings that need replacing. The result=$(echo "$str" | sed "s/$oldstr/$newstr/") replaces the first occurrence of the input string.

Replace the first occurrence of a string in bash

The image shows that the script has replaced the first occurrence of “new” with “replaced”.

D. Replace Multiple Occurrences

To replace multiple occurrences you have to use theg flagto perform the replacement globally. Check out the following script:

#!/bin/bash

# This script uses sed to replace multiple occurrences of a string
input="apple orange banana orange grapefruit orange"
pattern="orange"
replacement="kiwi"

# Use sed to replace all occurrences of 'orange' with 'kiwi'
modified_string=$(echo "$input" | sed "s/$pattern/$replacement/g")

# Print the modified string
echo "$modified_string"
EXPLANATION

Firstly the script declares the input string which contains multiple occurrences of a string named orange. Then the script also declares the replacement string namedkiwi. As the syntax"s/$pattern/$replacement/g"uses the g flag the script replaces all the occurrences of the input string.

Replace multiple occurrences using sed command in bash

The script has successfully replaced all the occurrences of “orange” with “kiwi.”

E. Replace Digit Pattern Using Regular Expression

To replace a digit pattern in a string you can use the regular expression with the sed command. The regular expressions mainly match the pattern in text data. For matching and replacing the pattern of a digit you can use the character classes[], negated character class [^] syntax. Check the following script:

#!/bin/bash

# First code to match 1
input='123, 456, 789, 12345'
output=$(echo "$input" | sed 's/1[^ ]*/replaced_string/g')

echo "Output: '$output'"

# Second code to match pattern
str="123, 456, 789, 12345"

result=$(echo "$str" | sed 's/[0-9][0-9]*/replaced_string/g')

echo "Original String :  $str"
echo "Replaced String :  $result"
EXPLANATION

In the first code, the script declares a string that contains multiple numbers having the digit 1. The pipe (|) operator takes the output from theechocommand to thesedcommand. Thesed 's/1[^ ]*/replaced_string/g'replaces the character that matches with1followed by non-space any character. In the second code, [0-9][0-9]*matches one or more digits and replaces them with the replaced_string variable using the sed command. The script replaces multiple occurrences using thegflag.

Match and replace the digit strings using the sed command

From the output you can see, that the script has replaced the digits that start with1and matched one or more digits and replaced them with “replaced_string”.

F. Replace Bash String in a File

Using the sed command you can easily replace a string in a file. If you use the sed command following the filename then you can replace any string from the file according to your preferences. Check the following script:

#!/bin/bash

# Take the search string
read -p "Enter the search string: " search

# Take the replace string
read -p "Enter the replace string: " replace

if [[ $search != "" && $replace != "" ]]; then
sed -i "s/$search/$replace/gi" $1
fi
EXPLANATION

At first, thereadcommand with thepoption prompts the user to enter the string to search and replace and store the input in the search and replace variable. The conditional statement[[ $search != "" && $replace != "" ]]checks if both the string has a non-empty value. If the statement is true then the script executes the if block. Later, thesedcommand performs a global case-insensitive replacement on the file which is specified with$1the first command line argument.

Using the sed command strings are replaced in a file

From the image you can see, that the script has taken the user input and replaced the searched string yellow with another input string pink in the file colors.txt.

3. Using “awk” Command

To replace a string with another in bash, you can use the awk command. The awk is a versatile command tool used for text processing, handling multi-byte characters, and especially for pattern matching and manipulation. The awk command simply takes the input and uses the gsub function to replace all the occurrences of the old string with the new string.

Here’s the script to replace a bash string using theawkcommand:

#!/bin/bash

# This script replaces the string "old_string" with "new_string" in the input string
input="This is an example with old_string and some other text"

# Use awk to replace "old_string" with "new_string"
modified_string=$(echo "$input" | awk '{gsub(/old_string/, "new_string"); print}')

echo "$modified_string"
EXPLANATION

Firstly, the script declares the input string in a variable namedinput. In theawkcommand using the global substitution (gsub), the script replaces all the occurrences of the old string with the new_string. Lastly, theechocommand prints the modified output.

Replace string using the awk command

The output shows the script has replaced the “old_string” with the “new_string”.

4. Using “perl” Command

In bash scripting, the perl command provides a straightforward method to replace a string within a file. For the in-place modification of a file, the Perl command utilizes the-pioption that directly allows to modification of the file. Using this command, you can also store the original files’ content.

To replace a string usingperlcommand follow the script below:

#!/bin/bash

# Check if the file exists
if [ -e "countries.txt" ]; then
# Perform the replacement using perl

perl -pi.bak -e 's/Armenia/Angola/g' countries.txt

echo "Replacement completed. Backup created: countries.txt.bak"

else
echo "Error: countries.txt not found."
fi
EXPLANATION

At first, the script checks whether the input file exists or not using the conditional statement. If the statement is true then the script executes the if block. Inside the if statement, the perl command uses -pi.bak option to directly modify the file and create a backup file. With the -e 's/Armenia/Angola/g' the script replaces all the occurrences of “Armenia” with “Angola” in the file.

Replace a string using perl command

From the image, you can see the script has replaced the string “Armenia” with “Angola” in the file and also stored the original content in the file named countries.txt.bak.

5. Using “tr” Command

To replace a character in a bash string you can also use the tr command. In bash scripting, the tr command is primarily used to translate and delete characters. But if you want to replace one individual character of a set with another set then you can use thetrcommand. Check the following script to replace the string:

#!/bin/bash

# This script replaces the character 'H' with 'J' in the string "Hello, World!"

echo "Hello, World!" | tr 'H' 'J'
EXPLANATION

The script declares the input with theechocommand. Then thepipeoperator takes the output of the echo command to the tr command. The tr ‘H’ ‘J’ syntax replaces the character H with another character J.

Replace a string using the tr command

From the output, you can see the script has replaced the character ‘H’ with another character ‘J’.

Note: With the tr command, you can only replace a single character or the same length of characters. If you are trying to replace the string “Hello” with “J” then it will not show the expected result.

Conclusion

To sum up, the article shows multiple methods to replace a bash string. While modifying a string in a file, external commands like sed, awk, and perl show a versatile result. If you want to use only pure bash scripting to replace a string then you can use the parameter expansion. Otherwise, to replace a single character, simply use the tr command. Hope the article will help you to understand and learn the basics of how to replace a bash.

People Also Ask

How to handle the case of string while replacing in bash?

To handle the case while replacing a string you have to use the-ioption. Use the-ioption with thegflag like-gi. Then this will change all the occurrences of the string whether it is in the uppercase or lowercase.

How to replace the whitespaces in a string?

To replace the white spaces with non-space string () you can use the sed command with character class []. The[[:space:]]indicates any white space character and the//indicates no space character (// means replace nothing). If you use thesedcommand following with [[:space:]], // andgflag then it will globally replace all the white space in a string. Check the following code:

#!/bin/bash

echo '  Hello,  World!  ' | sed 's/[[:space:]]//g'
#Output: Hello,World!

How to replace one substring for another string in the shell script?

To replace one substring with another substring for a string in the shell script, you can use the parameter expansion. In the parameter expansion, you need to use the double quote around the variable substitution of the second string if it contains spaces. Check the following script:

#!/bin/bash

firstString="I love Sushi and Tofu"
secondString="Tempura"

echo "${firstString/Sushi/"$secondString"}"

#Output: I love Tempura and Tofu

How do you replace a substring with an empty string in Bash?

To replace a string with an empty string in bash you can use parameter expansion. For replacing all the occurrences of a specific string with an empty string, use//syntax at the beginning of the substring to replace. Check the script below:

#!/bin/bash

# Original string
originalString="This is an example."

# Substring to be replaced
substringToReplace="example"

# Replace the substring with an empty string
result="${originalString//$substringToReplace/}"

# Print the result
echo "$result"

#Output: This is an .

Related Articles


<< Go Back to String Manipulation in Bash | Bash String | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
icon linux
Afia Zahin Oishi

Assalamualaikum, I am Afia Zahin, completed my graduation in Biomedical Engineering from Bangladesh University of Engineering and Technology, currently working as a Linux Content Developer Executive at SOFTEKO. A high achieving professional with a strong work ethic and able to work in a team in order to consistently achieve my goal and build my skillset. Able to handle difficult problems with patience and swift decision-making. Read Full Bio

Leave a Comment