Published: July 22, 2026
Read: 10 min
In: Technology & Development

TL;DR

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"
modified="${original/world/earth}"
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.

<strong>modified_text=${original_text/brown/green}</strong>

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:

<strong>modified_text=${original_text//brown/green}</strong>

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.