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:
- Using Parameter expansion:
${String/pattern/replacement}
- Using the sed command:
sed 's/pattern/replacement/' filename
- Using the awk command:
"input_str" | awk 'pattern { action }'
- Using the perl command:
perl [options] -e 'action' filename
- 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"
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.
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"
At first, with ${newstr}
, the script accesses the input string. Then it specifies the substitution pattern/old_string/replace_string
which replaces the string old_string with replaced-string.
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"
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”.
The 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}"
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_replace
at 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.
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 thesed
command, 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"
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.
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"
In the $(echo "$input" | sed 's/H/X/g; s/o/!/g')
, thepipe
operator takes the output of theecho
command to thesed
command. Here thes/H/X/g; s/o/!/g
replaces all occurrences of H with X and o with ! because of the -g flag.
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"
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.
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 flag
to 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"
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.
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"
In the first code, the script declares a string that contains multiple numbers having the digit 1
. The pipe (|) operator takes the output from theecho
command to thesed
command. Thesed 's/1[^ ]*/replaced_string/g'
replaces the character that matches with1
followed 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 theg
flag.
From the output you can see, that the script has replaced the digits that start with1
and 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
At first, theread
command with thep
option 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, thesed
command performs a global case-insensitive replacement on the file which is specified with$1
the first command line argument.
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.tx
t.
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 theawk
command:
#!/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"
Firstly, the script declares the input string in a variable namedinput
. In theawk
command using the global substitution (gsub), the script replaces all the occurrences of the old string with the new_string. Lastly, theecho
command prints the modified output.
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-pi
option that directly allows to modification of the file. Using this command, you can also store the original files’ content.
To replace a string usingperl
command 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
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.
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 thetr
command. 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'
The script declares the input with theecho
command. Then thepipe
operator takes the output of the echo command to the tr command. The tr ‘H’ ‘J’ syntax replaces the character H
with another character J
.
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-i
option. Use the-i
option with theg
flag 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 thesed
command following with [[:space:]], // andg
flag 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
- How to Convert a Bash String to Int? [8 Methods]
- How to Format a String in Bash? [Methods, Examples]
- How to Convert Bash String to Uppercase? [7 Methods]
- How to Convert Bash String to Lowercase? [7 Methods]
- How to Trim String in Bash? [6 Methods]
- How to Truncate String in Bash? [5 Methods]
- How to Remove Character from String in Bash? [7+ Methods]
- How to Remove First Character From Bash String? [7 Methods]
- How to Remove Last Character from Bash String [6 Methods]
- How to Use String Functions in Bash? [Examples Included]
- How to Generate a Random String in Bash? [8 Methods]
- How to Manipulate Bash String Array [5 Ways]
- How to Use “Here String” in Bash? [Basic to Advance]
- Encode and Decode with “base64” in Bash [6 Examples]
- How to Use “eval” Command in Bash? [8 Practical Examples]
- How to Get the First Character from Bash String? [8 Methods]
<< Go Back to String Manipulation in Bash | Bash String | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners