How to Use Bash to Replace Character in String

Meenakshi Agarwal
By
Meenakshi Agarwal
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large...
14 Min Read

If you’re working with Bash, understanding how to replace characters in a string is a handy skill. Let’s dive into this topic in simple terms with various methods and plenty of examples.

Multiple Ways to Replace Characters in String Using Bash

Learning bash scripting is an amazing skill for those working on Linux. As a Linux user, you may face a challenge like using bash to replace characters in a string. For this, there are multiple solutions that we brought into this tutorial. Check each of these methods one after the other.

1. Use Parameter Expansion

Bash Parameter Expansion is a powerful feature that allows you to manipulate variables, and it includes a syntax for replacing characters within a string. The general pattern for character replacement using parameter expansion is:

Basic Syntax

${var/pattern/replacement}

Here’s a breakdown of each component:

  • ${var}: This represents the variable containing the main string.
  • /: This is the delimiter that separates the variable, pattern, and replacement.
  • pattern: This is the substring or character you want to replace.
  • replacement: This is the new substring or character that will replace the old one.

Let’s explore this with a few examples:

Example1: Simple Character Replacement

orig="hello"
mod="${orig/o/O}"
echo $mod  # Output: hellO

In this example, we’re replacing the first occurrence of ‘o’ with ‘O’ in the variable orig.

Example2: Replace All Occurrences

orig="banana"
mod="${orig//a/X}"
echo $mod  # Output: bXnXnX

Here, we’re replacing all occurrences of ‘a’ with ‘X’ in the variable orig.

Example3: Replace Substring

orig="abcdefg"
mod="${orig/abc/XYZ}"
echo $mod  # Output: XYZdefg

This example demonstrates replacing the substring 'abc' with ‘XYZ’ in the variable orig.

Example4: Conditional Replacement

orig="apple"
mod="${orig/a/}"  # Replace first 'a' with nothing
echo $mod  # Output: pple

In this case, we’re replacing only the first occurrence of ‘a’ with an empty string, effectively removing it.

Example5: Replace Only at the Beginning

orig="apple"
mod="${orig/#a/X}"  # Replace 'a' only if it's at the beginning
echo $mod  # Output: Xpple

Using /# ensures that the replacement only occurs if ‘a’ is at the beginning of the string.

Example6: Replace Only at the End

orig="apple"
mod="${orig/%e/X}"  # Replace 'e' only if it's at the end
echo $mod  # Output: applX

With /%, the replacement happens only if ‘e’ is at the end of the string.

Bash Parameter Expansion provides a concise and convenient way to perform character replacements within strings, offering various options for different scenarios. It’s a valuable tool in your Bash scripting toolkit.

2. Using Sed to Replace Character in Bash

Sed, short for stream editor, is a powerful command-line tool for text processing and manipulation. It’s particularly useful for replacing characters or patterns in strings within files or streams. Let’s explore how to use Sed to replace characters in a string:

Basic Syntax

echo "orig_str" | sed 's/pattern/replacement/'

Here’s a breakdown of the components:

  • orig_str: The input string you want to modify.
  • pattern: The substring or regular expression you want to replace.
  • replacement: The new substring that will replace the matched pattern.

Example1: Simple Character Replacement

orig="hello"
mod=$(echo $orig | sed 's/o/O/')
echo $mod  # Output: hellO

In this example, we’re using sed to replace the first occurrence of ‘o’ with ‘O’ in the variable orig.

Example2: Replace All Occurrences

orig="banana"
mod=$(echo $orig | sed 's/a/X/g')
echo $mod  # Output: bXnXnX

Here, we’re replacing all occurrences of ‘a’ with ‘X’ in the variable orig using the global (g) flag.

Example3: Replace Substring

line="This is a sample line."
mod=$(echo $line | sed 's/is/at/')
echo $mod  # Output: That is a sample line.

This example demonstrates replacing the first occurrence of ‘is’ with ‘at’ in the variable line.

Example4: Using Variables

pattern='apple'
rep='orange'
orig="I have an apple."
mod=$(echo $orig | sed "s/$pattern/$rep/")
echo $mod  # Output: I have an orange.

You can use variables for the pattern and replacement to make your sed command more dynamic.

Example5: In-Place Editing of a File

# Replace 'old' with 'new' in a file.txt (in-place)
sed -i 's/old/new/' file.txt

The -i flag allows in-place editing of the file. Be cautious, as this modifies the file directly.

Example6: Case-Insensitive Replacement

orig="Hello World"
mod=$(echo $orig | sed 's/hello/hi/i')
echo $mod  # Output: hi World

Using the i flag makes the replacement case insensitive.

Sed provides a flexible and efficient way to replace characters or patterns in strings. It’s especially handy for automating text transformations in scripts and one-liners. Remember to adapt the command based on your specific use case and requirements.

3. Run Awk to Replace Character in a String

Awk is a powerful programming language for text processing, and it can be used to replace characters or patterns in strings within a Bash script. Let’s explore how to use Awk for character replacement:

Basic Syntax

echo "orig_string" | awk '{gsub(/pattern/, "replacement"); print}'

Here’s a breakdown of the components:

  • orig_string: The input string you want to modify.
  • /pattern/: The regular expression or pattern you want to replace.
  • "replacement": The new string that will replace the matched pattern.
  • {gsub(...)}: The Awk function for global substitution.
  • print: Outputs the mod string.

Example1: Simple Character Replacement

orig="hello"
mod=$(echo $orig | awk '{gsub(/o/, "O"); print}')
echo $mod  # Output: hellO

In this example, we’re using Awk to replace all occurrences of ‘o’ with ‘O’ in the variable orig.

Example2: Replace Substring

st="This is a sample word."
mod=$(echo $st | awk '{gsub(/is/, "at"); print}')
echo $mod  # Output: That at a sample word.

Here, we’re replacing the first occurrence of ‘is’ with ‘at’ in the variable st.

Example3: Using Variables

pat='apple'
rep='orange'
orig="I have an apple."
mod=$(echo $orig | awk -v pat="$pat" -v rep="$rep" '{gsub(pat, rep); print}')
echo $mod  # Output: I have an orange.

You can use the -v option to pass variables into Awk for dynamic replacements.

Example4: Customizing Output Format

orig="123-456-789"
mod=$(echo $orig | awk '{gsub(/-/, " "); print}')
echo $mod  # Output: 123 456 789

In this case, we’re replacing hyphens with spaces and customizing the output format.

Example5: Replace Only in Specific Fields

data="John,Doe,30"
mod=$(echo $data | awk 'BEGIN{FS=OFS=","} {gsub(/Doe/, "Smith", $2); print}')
echo $mod  # Output: John,Smith,30

Awk allows you to specify the field separator (FS) and output field separator (OFS) and target replacements within specific fields.

Awk provides a flexible and expressive way to replace characters or patterns in strings. It’s particularly useful when dealing with structured text data. Customize your Awk commands based on the complexity and requirements of your specific use case.

4. Make Use of tr Command

The tr command in Bash is a simple yet effective tool for translating or deleting characters. It’s commonly used for basic character replacements in strings. Let’s explore how to use the tr command for character replacement:

Basic Syntax

echo "orig_str" | tr 'old_char' 'new_char'

Here’s a breakdown of the components:

  • orig_str: The input string you want to modify.
  • 'old_char': The character you want to replace.
  • 'new_char': The character that will replace the old one.

Example 1: Simple Character Replacement

orig="hello"
mod=$(echo $orig | tr 'o' 'O')
echo $mod  # Output: hellO

In this example, we’re using tr to replace all occurrences of ‘o’ with ‘O’ in the variable orig.

Example2: Replace Multiple Characters

orig="abc123"
mod=$(echo $orig | tr 'a-c1-3' 'X')
echo $mod  # Output: XXXXXX

Here, we’re replacing characters ‘a’ through ‘c’ and digits ‘1’ through ‘3’ with ‘X’.

Example3: Replace with Space

st="Hello-World"
mod=$(echo $st | tr '-' ' ')
echo $mod  # Output: Hello World

This example replaces hyphens with spaces in the variable sentence.

Example4: Translate Sets of Characters

text="apple"
mod=$(echo $text | tr 'a-z' 'A-Z')
echo $mod  # Output: APPLE

Using character ranges, we’re translating lowercase letters to uppercase in the variable text.

Example5: Delete Characters

orig="remove-vowels"
mod=$(echo $orig | tr -d 'aeiou')
echo $mod  # Output: rmv-vwls

The -d option deletes specified characters (vowels in this case) from the variable orig.

Example6: Replace Spaces with Underscores

phrase="Hello World"
mod=$(echo $phrase | tr ' ' '_')
echo $mod  # Output: Hello_World

Here, we’re replacing spaces with underscores in the variable phrase.

The tr command is efficient for simple character replacements and is suitable for scenarios where you need to perform basic transformations on a string. Customize your tr command based on your specific replacement needs and the characters involved.

5. Using Substring in Bash to Replace Character

In Bash, you can replace characters within a substring using parameter expansion. This method allows you to target a specific portion of the string rather than replacing characters globally. Let’s explore how to use substring replacement in Bash:

Basic Syntax

${variable/pattern/replacement}

Here, variable is your orig string, pattern is the substring or character you want to replace, and replacement is the new substring or character that will replace the old one.

Example1: Simple Substring Replacement

orig="hello world"
mod="${orig/lo/LA}"
echo $mod  # Output: helLA world

In this example, we’re replacing the first occurrence of the substring ‘lo’ with ‘LA’ in the variable orig.

Example2: Replace Substring Globally

orig="apple orange apple"
mod="${orig//apple/APPLE}"
echo $mod  # Output: APPLE orange APPLE

Here, we’re replacing all occurrences of the substring ‘apple’ with ‘APPLE’ in the variable orig.

Example3: Replace at the Beginning

filename="file.txt"
mod="${filename/#file/Document}"
echo $mod  # Output: Document.txt

Using /# ensures that the replacement only occurs if ‘file’ is at the beginning of the string in the variable filename.

Example4: Replace at the End

filename="image.jpg"
mod="${filename/%jpg/png}"
echo $mod  # Output: image.png

With /%, the replacement happens only if ‘jpg’ is at the end of the string in the variable filename.

Example5: Dynamic Substring Replacement

substring='abc'
replacement='XYZ'
orig="abcdefg"
mod="${orig/$substring/$replacement}"
echo $mod  # Output: XYZdefg

You can use variables for the substring and replacement to make your substring replacement dynamic.

Example6: Replace Only if Another Substring is Present

orig="abcdefg"
substring='abc'
replacement='XYZ'
mod="${orig/$substring/${orig/*def*/$replacement}}"
echo $mod  # Output: XYZg

Here, the replacement occurs only if ‘def’ is present in the variable orig after the substring 'abc'.

Substring replacement in Bash provides a precise way to modify specific parts of a string. Customize the command based on your use case, ensuring that the pattern you’re matching accurately reflects the substring you want to replace.

6. Nested Parameter Expansion in Bash to Replace Character

As of my last knowledge update in January 2022, Bash does not support true nested parameter expansion. However, you can achieve a similar effect by using multiple parameter expansions sequentially. Let me demonstrate a method that simulates nested behavior for character replacement in a string.

Example

orig="abcdefg"
outer='abc'
inner='def'
replacement='XYZ'

# Perform outer replacement
mod="${orig/$outer/}"

# Perform inner replacement (within the result of outer replacement)
mod="${mod/$inner/$replacement}"

echo $mod  # Output: XYZg

In this example, we first perform an outer replacement to remove the substring specified by outer. Then, within the result of the outer replacement, we perform an inner replacement to replace the inner with the desired replacement. The final result is XYZg.

This method allows you to achieve a sequential replacement effect, simulating the concept of nested parameter expansion.

It’s essential to be cautious when using this approach, as the order of replacement matters. Ensure that the outer replacement doesn’t inadvertently affect the subsequent replacements.

In a nutshell, mastering character replacements in Bash is crucial for effective scripting. Choose the method that suits your task complexity, and stay updated on the latest Bash features for enhanced functionalities. Happy scripting!

Share This Article
Leave a Comment

Leave a Reply

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