> ## Content Index
> Fetch the complete content index at: https://linuxhandbook.com/llms.txt
> Use this file to discover other available public pages before exploring further.

# Replacing a Substring With Another String in Bash
- URL: https://linuxhandbook.com/replace-string-bash/
- Published: 2021-09-24T05:04:20.000Z
- Updated: 2022-11-13T01:44:09.000Z
- Description: Learn how to replace a single or multiple occurrences of a substring inside a string in Bash.
- Author: Avimanyu Bandyopadhyay
- Tags: Bash Tips

Here's the scenario. You have a big string and you want to replace part of it with another string.

For example, you want to change "I am writing a line **today**" to "I am writing a line **now**".

In this quick tutorial, I'll show you how to replace a substring natively in Bash. I'll also show the sed command example as an extension.

## Replace substring natively in bash (good for a single line)

Bash has some built-in methods for string manipulation. If you want to replace part of a string with another, this is how you do it:

```
${main_string/search_term/replace_term}
```

Create a string variable consisting of the line: “I am writing a line today” without the quotes and then replace `today` with `now`:

```
iborg@iborg-desktop:~$ line="I am writing a line today"
iborg@iborg-desktop:~$ echo "${line/today/now}"
I am writing a line now

```

Did you understand what just happened? In the syntax `"${line/today/now}"`, line is the name of the variable where I've just stored the entire sentence. Here, I'm instructing it to ***replace the first occurrence*** of the word `today` with `now`. So instead of displaying the contents of the original variable, it showed you the line with the changed word.

Hence, the `line` variable hasn't actually changed. It is still the same:

```
iborg@iborg-desktop:~$ echo $line
I am writing a line today

```

But you can definitely replace the word you want to and modify the same variable to make the changes permanent:

```
iborg@iborg-desktop:~$ line="${line/today/now}"
iborg@iborg-desktop:~$ echo $line
I am writing a line now

```

Now the changes have been made permanent and that's how you can permanently replace the first occurrence of a substring in a string.

You can also use other variables to store specific substrings that you wish to replace:

```
iborg@iborg-desktop:~$ replace="now"
iborg@iborg-desktop:~$ replacewith="today"
iborg@iborg-desktop:~$ line="${line/${replace}/${replacewith}}"
iborg@iborg-desktop:~$ echo $line
I am writing a line today

```

Here, I stored the word to be replaced in a variable called `replace` and the word that it would be replaced with inside `replacewith`. After that, I used the same method as discussed above to “revise” the line. Now, the changes I had made in the beginning of this tutorial have been reverted.

Let us look at another example:

```
iborg@iborg-desktop:~$ hbday="Happy Birthday! Many Many Happy Returns!"
iborg@iborg-desktop:~$ hbday="${hbday/Many/So Many}"
iborg@iborg-desktop:~$ echo $hbday
Happy Birthday! So Many Many Happy Returns!

```

### Replacing all occurrences of a substring

You can also replace multiple occurrences of substrings inside strings. Let's see it through another example:

```
iborg@iborg-desktop:~$ hbday="${hbday//Many/So Many}"
iborg@iborg-desktop:~$ echo $hbday
Happy Birthday! So Many So Many Happy Returns!

```

That extra `/` after `hbday` made it replace all occurrences of `Many` with `So Many` inside the sentence.

[Learn Bash Quickly - Bash Beginner Book for Linux UsersThis book is available for FREE to Linux Handbook members.Learn Bash Quickly will teach you everything you need to get started with bash scripting. Each bash concept is explained with easy to understand examples. You’ll learn to:Create and run a bash scriptUse variables and pass arguments to scriptU…![](https://public-files.gumroad.com/variants/mkb3qijws0s0gb0g9l2wxggvscph/4ec519eb32080d4ff1ef08cba157dc2ac7dab092fa26aeca54e8e2b8f31f9a63)Gumroad![](https://public-files.gumroad.com/variants/hvey1w1iuswsaqiaabvq75ncq4s1/3953c12f08a92d2317e5f6a4fe2f6dd48702c3beec8a2173335cffa31a3561ec)](https://linuxhandbook.gumroad.com/l/ejfGu?ref=linuxhandbook.com)

## Replace string using sed command (can work on files as well)

Here's another way for replacing substrings in a [string in bash](https://linuxhandbook.com/bash-strings/). [Use the sed command](https://linuxhandbook.com/sed-command-basics/) in this manner:

Replace the first occurrence:

```
line=$(sed "s/$replace/s//$replacewith/" <<< "$line")
```

If I take the first example, it can be played as:

![Replace bash substring with sed](https://linuxhandbook.com/content/images/2021/09/bash-replace-string-sed.png)

You can also replace all occurrences by adding `g` at the end:

```
line=$(sed "s/$replace/$replacewith/g" <<< "$line")
```

Now, you may think this is more complicated than the native bash string method. Perhaps, but sed is very powerful and you can use it to replace all the occurrences of a string in a file.

```
sed -i 's/$replace/$replacewith/' filename
```

Here's an example of some of Agatha Christie's books with 'Murder' in the title:

```
iborg@iborg-desktop:~$ cat agatha.txt 
The Murder in the Vicarage
Murder in Mesopotamia
Murder is Easy
Murder on the Orient Express
Murder In Retrospect

```

I am going to replace Murder with Marriage because some people think both are the same:

```
sed -i "s/Murder/Marriage/g" agatha.txt
```

And here's the changed file now:

```
The Marriage in the Vicarage
Marriage in Mesopotamia
Marriage is Easy
Marriage on the Orient Express
Marriage In Retrospect
```

![](https://linuxhandbook.com/content/images/2021/09/sed-replace-string-in-file.png)

Sed is a very powerful tool for editing text files in Linux. You should at least learn its basics.

[Getting Started With SED Command \[Beginner’s Guide\]Learn to use one of the most powerful commands of the Unix toolbox: sed, the stream editor with practical examples of SED commands.![](https://linuxhandbook.com/favicon.png)Linux HandbookSylvain Leroux![](https://linuxhandbook.com/content/images/2020/07/sed-commands-featured-1.jpeg)](https://linuxhandbook.com/sed-command-basics/)

Now that you know about replacing substrings, how about [splitting strings into substrings in bash](https://linuxhandbook.com/bash-split-string/)?

Did you enjoy reading this article? If you have anything to share about replacing strings or this article, please do so in the comments below.