How to replace string and its all occurrence with another string using bash

Bash supports number of string manipulation operations. It is providing the option to replace the given string to another string. String replacement using another string is commonly required in string processing and shell scripting program.

Syntax for replace string using Bash shell script

Example 1: Replace the string in Bash

In this example, we are going to replace the string ‘is’ to ‘was’ using bash shell script. The input string is assigned to Input_String variable. Then we are replace the string ‘is’ to ‘was’ and store it in the Output_string variable. Lets run the shell script and see the output as below.

Output

Example 2 : Replace the string with single quotes

Here we are replacing = (equal) character with =’ (equal with single quotes) characters. \(backlash) is used to escape single quote that represent it as single quote character instead of string or command opening and closing character.

we used single quote to cover input string that avoid misinterpretation of special characters by shell.

Output

Recommended Articles