Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He...Read more
TL;DR
Here are the six ways to replace string in Bash:
- Use the sed command to replace a string in Bash by executing
sed 's/original/replacement/g' input-file > output-file
. - Run the awk command to replace a string in Bash by executing
awk '{gsub(/original/, "replacement")}1' input-file > output-file
. - Try the tr command to replace a string in Bash by executing
tr 'original' 'replacement' < input-file > output-file
. - Execute the Bash built-in functions for string manipulation, such as
${myString/brown/red}
to replace the first occurrence of “brown” with “red”. - Use regular expressions to replace strings in Bash by executing
sed 's/[0-9]/X/g' input-file > output-file
orawk '{gsub(/[0-9]/, "X")}1' input-file > output-file
. - Create Bash scripts to automate string replacement tasks by creating a script file with the code and executing it with
./replace_string.sh input-file search-string replacement-string output-file
.
To replace strings in Bash efficiently, use built-in functions for simple tasks and sed or awk for larger files. Test commands on sample data and keep scripts modular. Use -n with sed and p commands to print specific lines in large files.
Bash is a popular Unix shell that offers powerful tools for string manipulation, such as modifying, extracting, or replacing parts of a string in a text file. This is useful when Linux programmers need to find and replace string in Bash in a variety of scenarios, such as data processing, system administration, log analysis, automation tasks, and more. So, in this comprehensive guide, you’ll get to know various techniques to replace strings in Bash that may come in handy to you as a Linux programmer.
How to Replace String in Bash [6 Easy Ways]
To replace string in Bash, you can use sed, awk, tr, and regex methods. Additionally, built-in string manipulation functions and Bash scripting can be used for automated string replacement. Here, I’ll provide step-by-step instructions for each of these methods:
1. sed Command
sed (stream editor) is a powerful text processing tool in Unix-based systems, primarily used for text substitution. Here’s how to use it:
- Launch the Terminal app and execute the command below to read the text file:
cat filename
- Now, run the command below to replace string in Bash.
sed 's/original/replacement/g' input-file > output-file
- This command will replace all occurrences of “original” with “replacement”. Now, you’ll see the following output using the cat command:


2. awk Command
awk is a versatile text processing tool that operates with patterns and actions. You can use this command to replace string in Bash in the following way:
- Execute the command below to read the text file in the Terminal window:
cat filename
- Run the following command in the Linux command prompt:
awk '{gsub(/original/, "replacement")}1' input-file > output-file
- Now, you’ll see all occurrences of “string 1” replaced with “string 2”:


3. tr Command
tr (translate) is a command for character-level translations and deletions. Follow these steps to use it to replace string in Bash:
- Run the
cat
command in the command prompt to read the file.
cat filename
- Enter the following command in the Terminal window:
tr 'original' 'replacement' < input-file > output-file
tr 'aeiou' 'AEIOU' < text.txt > text_modified.txt
will find and replace all lowercase vowels with their uppercase counterparts. Here is the output:


4. Bash Built-In Functions
Bash provides built-in string manipulation functions using parameter expansion, substring replacement, and case modification. To replace string in Bash, try these built-in functions:
- Save the string “The quick brown fox jumps over the lazy dog” in a variable named “myString” with the following command:
myString="The quick brown fox jumps over the lazy dog"
- Now, you can use the variable “myString” in other commands to manipulate or display its contents. To confirm the variable value, run the
echo $myString
command in the Linux command prompt:


- To replace only the first occurrence of a substring “brown” with “red” in the string, you can use the following command:
echo ${myString/brown/red}
- The output would be:


- You can also run the following command to replace all occurrences of a substring:
echo ${myString//e/a}
- If done correctly, it’ll replace string in Bash, and you’ll get the output in Terminal:


- You can also convert a string to uppercase with the following command:
echo ${myString^^}
- You’ll get the following output in the Terminal window.


- Similarly, if you want to replace string in Bash with its lowercase, run the following command:
echo ${myString,,}
- The output of this command would be:


5. Regular Expressions
Regular expressions (regex) are a powerful way to define search patterns in text. It is a sequence of characters that specifies a search pattern. It can be used to match, find, or replace specific strings within a text. Here’s how you can use it:
- Create and save the text file with the following content:
123456789
The quick brown fox jumps over the 9 lazy dogs.
- Launch the Terminal app and execute the
cat
command to confirm the text file content.


- Now, use the
sed
command below to replace all digits with an ‘X’:
sed 's/[0-9]/X/g' input-file > output-file
- Use the
cat
command to see the output in your Terminal window.


- Alternatively, you can use the
awk
command to replace all digits with an ‘X’:
awk '{gsub(/[0-9]/, "X")}1' input-file > output-file
- You’ll see the following output using the
cat
command:


6. Bash Scripting and Automation
You can create reusable scripts and automate the string replacement tasks to replace string in Bash. This can save time and reduce errors. To do so, follow these steps:
- Create a Bash script file:
touch replace_string.sh
- Execute the
nano
command with the filename.
nano replace_string.sh
- Add the following content to the script:
#!/bin/bash
input_file="$1"
search_string="$2"
replacement_string="$3"
output_file="$4"
sed "s/$search_string/$replacement_string/g" "$input_file" > "$output_file"
This script takes four arguments: the input file name, the search string, the replacement string, and the output file name. It uses the sed
command to replace all occurrences of the search string with the replacement string in the input file and saves the output to the specified output file.
- Press Ctrl + O and Enter to save. Then, press Ctrl + X to exit the Nano text editor.


- Make the script executable:
chmod +x replace_string.sh
- Execute the
cat
command to read the input file.
cat filename
- Run the script in the Terminal window:
./replace_string.sh input-file search-string replacement-string output-file
- Run the
cat
command to see the output:


4 Quick Tips to Replace String in Bash Efficiently
To replace string in Bash efficiently, use optimized techniques to do it. This is especially important when handling large datasets and complex automation processes. Consider the following four tips to optimize your string replacement techniques and improve your Bash scripting skills:
- 🚀 Speed considerations: For simple string replacement tasks, use built-in Bash functions, as they are generally faster than external tools like sed and awk. However, you may still have to use
sed
orawk
over Bash built-in functions for large files, as they handle the memory more efficiently. - 🧪 Test the string replacement: Always test your string replacement commands and scripts on sample data before applying them to critical files or production environments. Keep your scripts modular and reusable to reduce the likelihood of errors and improve maintainability.
- 💾 Memory usage: Be mindful of memory usage when working with large files or data sets.
sed
andawk
can be more memory-efficient than built-in Bash functions for such tasks. - 📄 Print specific lines: Use the
-n
option with sed to disable automatic printing of output, and then use thep
command to print only specific lines that match your search pattern. This can save time and resources when working with large files.
In Conclusion
By now, you should have a solid understanding of various techniques to replace string in Bash using the sed, awk, tr, and regex methods. You can further explore Bash string manipulation to help you automate tasks and streamline your workflow.
However, your learning journey in Bash scripting doesn’t end here. There are many other aspects to explore, such as improving code readability with echo and using set x for debugging. You can also look into multiple ways to clear Bash history to experience efficient Bash performance. Continuously learning and growing will lead to success in the world of Unix shell scripting.
Frequently Asked Questions
How do single and double quotes differ in Bash string manipulation?
Single quotes in Bash preserve the literal value of all characters within the quotes, meaning that variables and special characters are not interpreted or expanded. Double quotes, on the other hand, allow for variable expansion and interpretation of certain special characters.
How can I undo a string replacement operation in Bash?
It is recommended to make a backup copy of the original file before performing any string replacement operations. If a mistake is made, you can restore the original file from the backup. Alternatively, some tools like sed have an undo option (e.g., -i.bak
), which creates a backup file during string replacement that can be used to undo the operation.
Is it possible to perform a case-insensitive string replacement in Bash?
Yes, for case-insensitive string replacements, use sed with the I
flag within the s
command, like sed 's/search/replacement/gI'
. Alternatively, use awk with tolower()
and toupper()
functions to convert strings to the same case before comparison.
Can I perform string replacement on multiple lines or a block of text using Bash, sed, or awk?
Yes, string replacement on multiple lines or a block of text can be done in sed and awk by using regex patterns and options that match multiple lines or blocks of text. In Bash, use the tr or perl command with the s
option to perform the replacement on multiple lines or a block of text.
How can I replace strings in multiple files at once using Bash, sed, or awk?
To replace strings in multiple files, use the find
command to locate the files and the xargs
command to pass them as input to sed or awk. For example, to replace “foo “with “bar” in all files with a .txt extension, use find . -name "*.txt" -print0 | xargs -0 sed -i 's/foo/bar/g'
.
Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.