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

Avatar

By squashlabs, Last Updated: October 20, 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 Echo a Newline 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

– 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.

Related Article: How to Use If-Else Statements in Shell Scripts

Locating and Moving Files in Bash Scripting on Linux

Learn how to locate and move files in Linux using bash scripting. This article covers file search, manipulation, handling, and navigation techniques, as well as ways to... 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 permissions of a... read more

How to Make a Bash Script Continue to Run After an Error

Bash scripts are a powerful tool for automating tasks in Linux systems. However, when errors occur, scripts often stop running, causing delays and inefficiencies. In... read more

Formatting and Displaying Dates with Bash Scripts in Linux

Learn how to format and display dates using bash scripts in Linux. This article covers basic and advanced formatting options, manipulating dates, extracting specific... read more

How to Check the Success of a Bash Script

Learn how to check the success of a bash script execution in a Linux environment. Dive into topics such as bash script success check, error handling, determining if a... read more

How to Extract Numbers from Strings in Bash

Learn how to extract numbers from strings using bash scripting on Linux. This article covers various techniques such as regular expressions, awk, cut, tr command, expr... read more