How to Replace a Substring in a String in a Shell Script

Avatar

By squashlabs, Last Updated: Oct. 21, 2023

How to Replace a Substring in a String in a Shell Script

To replace a substring in a string in a shell script, you can use various built-in string manipulation functions and techniques available in Linux bash. In this answer, I will provide two possible approaches to accomplish this task.

Approach 1: Using the sed Command

The sed command is a useful tool for stream editing in Linux. It can be used to search for a pattern in a file or a string and replace it with a new value. Here's how you can use sed to replace a substring in a string:

1. Make sure you have the sed command installed on your system. You can check if it's available by running the following command in your terminal:

which sed

2. Use the following syntax to replace a substring in a string using sed:

new_string=$(echo "$original_string" | sed 's/old_substring/new_substring/')

Here, original_string is the string that contains the substring you want to replace, old_substring is the substring you want to replace, and new_substring is the new substring you want to replace it with.

3. Replace the variables original_string, old_substring, and new_substring with the actual values in your script.

Here's an example that demonstrates the usage of sed to replace a substring in a string:

original_string="Hello, World!"new_string=$(echo "$original_string" | sed 's/World/Universe/')echo "$new_string"  # Output: Hello, Universe!

This example replaces the substring "World" with "Universe" in the original string "Hello, World!".

Related Article: How to Extract Substrings in Bash

Approach 2: Using Parameter Expansion

Another way to replace a substring in a string is by using parameter expansion, a feature provided by the bash shell. Here's how you can do it:

1. Use the following syntax to replace a substring in a string using parameter expansion:

new_string=${original_string//old_substring/new_substring}

Here, original_string is the string that contains the substring you want to replace, old_substring is the substring you want to replace, and new_substring is the new substring you want to replace it with.

2. Replace the variables original_string, old_substring, and new_substring with the actual values in your script.

Here's an example that demonstrates the usage of parameter expansion to replace a substring in a string:

original_string="Hello, World!"new_string=${original_string//World/Universe}echo "$new_string"  # Output: Hello, Universe!

This example replaces the substring "World" with "Universe" in the original string "Hello, World!".

Alternative Ideas and Best Practices

Related Article: Should You Use Numbers in Bash Script Names?

- If you only want to replace the first occurrence of the substring in the string, you can modify the sed command or parameter expansion syntax accordingly.

- The sed command supports regular expressions, so you can use more complex patterns for searching and replacing substrings.

- When using parameter expansion, be cautious about the use of the double slash (//) to replace all occurrences of the substring. If you only want to replace the first occurrence, use a single slash (/) instead.

How to Run Bash Scripts in the Zsh Shell

Bash scripts are a common tool used in the Linux environment for automating tasks. However, the Zsh shell has gained popularity as an alternative to … read more

Utilizing Variables from Bash Scripts in PHP on Linux

PHP developers often find themselves needing to access variables from bash scripts on Linux. This article explores the method for PHP to utilize thes… read more

How to Compare Two Files in Bash Script

Differentiating two files in a bash script on Linux can be done using various commands and techniques. In this article, we will explore different met… read more

How to Import JSON from a Bash Script on Linux

Learn how to import JSON data into a Bash script on Linux with detailed instructions. The article covers techniques for parsing JSON, libraries for p… read more

How To Find Files Based On Wildcard In Linux

Learn how to use the 'find' command in Linux to recursively search for files in current and subfolders using wildcards. Discover how to use the find … read more

Making Bash Scripts Executable with Chmod in Linux

Learn how to modify permissions in Linux to make bash scripts executable using chmod. Discover the different permissions in Linux, check the current … read more

How to Handle New Lines in Bash Scripts

New lines in bash scripts play a significant role in the Linux environment. Understanding the difference between line breaks and carriage returns, as… read more

How To Find All Files With Text On Linux

Learn how to search for specific text in files on Linux using simple commands. Find all files containing a text string easily and efficiently. Discov… read more

Handling Pytest Failures in Bash Script on Linux

The article is a detailed walk-through that explains how to make a bash script fail when pytest fails in a Linux environment. The article provides st… read more

Using Variables in If Statements in Bash Scripts

Using variables in if statements in bash scripts allows for dynamic decision-making within the script. This article explores the syntax, examples, be… read more