How to replace one substring for another string in shell script ?

To replace a substring with another string in a shell script, you can use built-in parameter expansion (Bash-specific) or external tools like sed. Below are the methods, examples, and use cases:

1. Using Bash Parameter Expansion

Ideal for simple substitutions in Bash scripts.

Replace First Occurrence

string="Hello World, Hello Universe"
new_string="${string/Hello/Hi}"
echo "$new_string"  # Output: "Hi World, Hello Universe"

Replace All Occurrences

string="apple orange apple banana"
new_string="${string//apple/mango}"
echo "$new_string"  # Output: "mango orange mango banana"

Replace Substrings with Special Characters

Escape / or use a different delimiter:

path="/usr/local/bin"
new_path="${path//\/local/\/share}"  # Escape slashes
echo "$new_path"  # Output: "/usr/share/bin"

2. Using sed (Stream Editor)

Flexible for complex patterns, case insensitivity, and multi-line strings.

Basic Replacement

string="Hello World, Hello Universe"
new_string=$(echo "$string" | sed 's/Hello/Hi/g')
echo "$new_string"  # Output: "Hi World, Hi Universe"

Case-Insensitive Replacement

string="The quick brown fox jumps over the lazy dog"
new_string=$(echo "$string" | sed 's/the/That/gi')  # 'i' flag for case insensitivity
echo "$new_string"  # Output: "That quick brown fox jumps over That lazy dog"

Replace with Different Delimiters (Avoid Escaping /)

path="/usr/local/bin"
new_path=$(echo "$path" | sed 's|/local|/share|g')
echo "$new_path"  # Output: "/usr/share/bin"

3. Handling Edge Cases

Variables with Spaces or Special Characters

Use quotes to preserve integrity:

str="File name with spaces.txt"
new_str="${str/ /_}"  # Replace first space with _
echo "$new_str"       # Output: "File_name with spaces.txt"

Multi-Line Strings

Use sed with -z to process the entire input as a single line:

multiline_str="Line one\nLine two\nLine three"
new_str=$(echo -e "$multiline_str" | sed -z 's/\n/ /g')  # Replace newlines with spaces
echo "$new_str"  # Output: "Line one Line two Line three"

Comparison of Methods

MethodProsCons
Bash Parameter ExpansionFast, no external tools needed.Limited to simple patterns; case-sensitive.
sedSupports regex, case insensitivity, and complex patterns.Slower for large data; requires piping.

Examples in Scripts

Replace in a Variable (Bash)

filename="image.jpg"
new_filename="${filename/.jpg/.png}"  # Replace .jpg with .png
echo "$new_filename"  # Output: "image.png"

Replace in a File (sed)

Replace all instances of old_text with new_text in a file:

sed -i 's/old_text/new_text/g' file.txt  # -i edits file in-place

Key Takeaways

  • Use Bash parameter expansion for simple, single-script substitutions.
  • Use sed for regex, case insensitivity, or multi-line replacements.
  • Escape special characters (e.g., /, &) in patterns with \ or use alternate delimiters (e.g., |).
  • Always quote variables to handle spaces and special characters safely.

By choosing the right method, you can efficiently manipulate strings in shell scripts!

Leave a Reply

Your email address will not be published. Required fields are marked *