3 Best Methods to Replace a Substring With Another String in Bash


To replace a substring with another string in Bash, you can try any of the following methods:

  • Bash’s parameter expansion allows for straightforward text replacement within a string, using ${original_text/brown/green} for a single instance or ${original_text//brown/green} for all occurrences, easily verifying changes with an echo command.
  • The sed command offers a powerful way to replace text, using $(sed 's/brown/green/' <<< "$original_text") for a single change or adding a g flag for global substitution, with results displayed using echo.
  • Awk’s gsub function enables global text replacement across a string, executed with $(awk '{ gsub(/brown/, "green"); print }' <<< "$original_text"), allowing for immediate verification of modifications through echoing the output.

Replacing substrings in Bash enhances data consistency, automates text editing, generates dynamic content, and facilitates bulk modifications, streamlining data management and customization. It’s a crucial skill for maintaining data integrity, reducing errors, and ensuring efficiency across various scripting and development tasks.

Read the guide below to learn three different methods for replacing substrings in Bash. Also, learn why this is important and what errors you encounter during the process.

Updating a word or phrase throughout your script can feel overwhelming, especially if you’re picturing doing it line by line. But for Linux users, there’s good news, it’s way easier than you think. In this post, I’ll start with an easy trick for text swapping and then explore how to use powerful tools like sed and awk for the more challenging edits. I’ll also highlight why these skills are incredibly useful and provide some handy tips to dodge common errors, ensuring your script editing process is smooth and hassle-free.

How to Replace a Substring With Another String in Bash?

To replace a substring with another string in Bash, you can use the string manipulation capabilities built into the shell. Specifically, you can use the syntax ${string/substring/replacement}. This will replace the first occurrence of substring in string with replacement. If you want to replace all occurrences of the substring, use ${string//substring/replacement}. Here’s a quick example:

original="hello world"
echo $modified  # Outputs: hello earth

Replacing a substring with another string in Bash is a common task that can be accomplished through various methods, each with its own use cases and advantages. Here are three effective methods to achieve this:

1. Parameter Expansion

Bash supports a feature known as parameter expansion, which allows for a variety of manipulations of the value of a variable directly within the shell. For substring replacement, follow these steps:

  1. Create a variable to store the string that needs modification.
<strong><code>original_text="The quick brown fox jumps over the lazy dog."</code></strong>

This command defines a variable named original_text containing the initial string.

storing string
  1. Execute the substring replacement using Bash’s parameter expansion.

This command replaces the first occurrence of brown with green in the original_text string, assigning the result to modified_text.

replacing substring
  1. Now run the following command to echo the modified text to the console to verify the replacement:
<strong>echo "$modified_text"</strong>

This prints the content of modified_text to the console, allowing you to see the result of the substitution.

replacing first occurrence
  1. To replace all occurrences, you’d use:

The double slashes (//) ensure that every instance of brown is replaced with green.

replacing all occurrences

2. sed Command

sed (Stream Editor) is a powerful Unix utility that performs text transformations on an input stream (a file or input from a pipeline). It’s not a Bash built-in, but it’s commonly used in shell scripts for its powerful pattern-matching and substitution capabilities. Here is the step-by-step guide:

  1. Define a variable for the original string.
<strong>original_text="The quick brown fox jumps over the lazy dog."</strong>

This line creates a variable named original_text that holds the string you intend to work with.

  1. Use the sed command for the substring replacement.
<strong>modified_text=$(sed 's/brown/green/' <<< "$original_text")</strong>

This sed command replaces the first occurrence of brown with green. The <<< operator is used to pass original_text as input to sed.

replacing first occurrence using sed command
  1. For replacing all occurrences:
<strong>modified_text=$(sed 's/brown/green/g' <<< "$original_text")</strong>

Adding the g flag at the end commands sed to replace all instances of brown with green.

  1. Output the result to the console.
<strong>echo "$modified_text"</strong>

Echoing modified_text displays the updated string, showing the effect of the sed operation.

replacing all occurrences using sed command

3. awk Command

awk is another powerful text processing tool available in Unix-like systems for pattern scanning and processing. It’s particularly useful for more complex text manipulations involving conditions, regular expressions, or when working with structured text (like CSV). To replace a substring using awk, you can use the gsub function:

  1. Set up a variable with the string.
<strong>original_text="The quick brown fox jumps over the lazy dog."</strong>

Here, you’re initializing original_text with the string that will undergo substitution.

  1. Carry out the replacement using awk.
<strong>modified_text=$(awk '{ gsub(/brown/, "green"); print }' <<< "$original_text")</strong>

This command uses awk’s gsub function to globally replace brown with green in the string. The <<< is a here-string that feeds original_text to awk.

replacing substring using awk command
  1. Print the modified string to confirm changes.
<strong>echo "$modified_text"</strong>

This final step prints the modified version of the string, allowing you to verify that the replacement has been successfully applied.

viewing modified output

4 Reasons Why You Need to Replace a Substring

Replacing a substring with another string in Bash is a simple yet powerful tool for text manipulation, offering flexibility and efficiency in handling data across various scenarios. Here are 4 reasons why you need to replace a substring with another Bash string:

  • 🔄 Data Normalization: Substring replacement in Bash is crucial for data normalization, ensuring consistency across datasets. For instance, replacing varied date formats with a standard one facilitates easier parsing and analysis. This uniformity is essential for accurate data comparison, aggregation, and reporting, enhancing data integrity and usability.
  • 🛠️ Automating Text Processing: Automating repetitive text editing tasks, such as updating configuration files or modifying codebases, saves time and reduces the risk of human error. Bash scripting offers a powerful way to automate these processes, making substring replacement an invaluable tool for system administrators and developers alike.
  • 📈 Dynamic Content Generation: Generating dynamic content based on specific conditions or inputs is a common requirement in scripting and development. Using Bash to replace substrings allows for the creation of customized messages, configurations, or files, adapting the output dynamically to meet user needs or application states.
  • 🔍 Search and Replace in Bulk: When dealing with large numbers of files, manual search-and-replace operations are impractical. Bash scripting enables bulk modifications, such as updating URLs or correcting common spelling errors across multiple documents, streamlining the process and ensuring consistency throughout your files or codebase.

Troubleshooting Common Errors Replacing a Substring With Another String

You might encounter several common errors when working with Bash to replace a substring with another string. Understanding these errors can help troubleshoot and ensure your script functions as intended. Here are five common errors to be aware of:

  • 🚫 Unescaped Special Characters: Special characters like *, &, /, or $ in substrings can cause syntax errors if not escaped properly. Use a backslash (\) to escape or single quotes for literals in tools like sed to ensure correct interpretation.
  • 📝 Variable Expansion Issues: Incorrect quoting can lead to unintended variable expansion or word splitting, particularly with spaces or special characters. Always encase variables in double quotes to prevent these issues, especially when using them in expressions or commands.
  • 🔠 Overlooking Case Sensitivity: Bash, sed, and awk are case-sensitive by default, which may lead to missed replacements if cases don’t match. Use the I flag in sed (s/pattern/replacement/Ig) for case-insensitive operations.
  • 🌟 Glob Pattern Misinterpretation: Bash might interpret patterns as glob patterns, leading to unexpected matches. Ensure patterns are correctly quoted to avoid unintended glob matching and test expressions with various inputs to catch issues.
  • ✂️ Using Single Slash for Global Replacement in Parameter Expansion: Forgetting to use double slashes (//) in Bash’s parameter expansion for global replacement (replacing all occurrences) and using a single slash (/) will only replace the first occurrence.

In a Nutshell

In this guide, you have learned how to change substrings in Bash, making your scripts smarter and your life a bit easier. I have covered everything from basic text changes to diving deep with commands like sed and awk, plus how to fix common errors. It’s all about making sure you can handle text like a pro, whether you’re tidying up data or customizing scripts.

If you’re eager to explore more, there are topics that you can learn about Bash arrays, getting clever with regular expressions, or setting up your own automated tasks. Each new skill you pick up will not only make your scripts better but also open up new possibilities for what you can do with them.

Frequently Asked Questions

  1. What Is Bash?

    Bash, short for Bourne Again SHell, is a command processor that typically runs in a text window, allowing the user to execute commands typed into a command line interface. It is widely available on various operating systems and is a default shell on most GNU/Linux systems. Bash is an enhancement of the original Bourne shell (sh), incorporating features from other shells like ksh and csh such as command line editing, unlimited size command history, job control, and shell functions. It’s highly favored for its programming interface as well as its interactive use, supporting both complex scripting and simple command execution.

  2. What is a Substring?

    A substring is a contiguous sequence of characters within a string. It is essentially a portion or segment of a string that you can extract from the main string based on specified positions. For example, in the string “hello”, “ell” is a substring. Substrings are useful in programming for tasks like searching, data manipulation, and pattern recognition within larger strings.

  3. Can I use Bash to replace substrings in binary files, or is it limited to text files?

    Bash primarily targets text file manipulation, yet replacing substrings in binary files is feasible under specific conditions. If the replacement maintains the original file size—meaning the new string is identical in length to the old one—it’s possible. Exercise caution, as improper modifications risk corrupting binary files. This approach is not commonly recommended for binary data handling.

  4. When using sed for in-place editing, how can I keep a backup of the original file?

    Using sed for in-place file editing includes an option to preserve a backup of the original file. By invoking sed with the -i option followed by a suffix, like -i’.bak’, sed edits the file while saving the unmodified version as a backup. For instance, executing sed -i'.bak' 's/old/new/g' file.txt generates file.txt.bak as a safety measure.

  5. How can I replace a substring that includes newline characters in Bash?

    Replacing substrings that contain or span newline characters requires careful handling. Utilize sed, employing patterns that specifically match newlines (\n), or adapt your approach to treat the newline character as a regular part of your text, perhaps by substituting it temporarily with a unique placeholder. This method allows for a broader range of replacements but necessitates attention to detail to avoid altering the intended content structure.



Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.



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.

Share this article
Shareable URL
Prev Post

How to Use Grep with OR Operator, AND Operator, and NOT Operator [5 Simple Methods]

Next Post

5 Ways To Use ls Command to Sort Files by Size in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *

Read next