Remove substrings from string using bash script – Code Example


In this article we will provide you the bash script code to remove all matching substrings from the provided string. You can use regex or simple string pattern as matching substring. Use this code in your projects without issues.

Code Example –

1. function –


string_remove_pattern() {
    echo "${1//$2}"

string_remove_pattern "I am not animal lover" "not"

# Output
# I am  animal lover

2. Removing a simple substring

string_remove_pattern "I am not animal lover not" "not"

# Output
# I am  animal lover

3. Removing all vowels

string_remove_pattern "remove all vowels from this" "[aeiou]"

# Output
# rmv ll vwls frm ths

4. Removing all numbers

string_remove_pattern "remove 86 from this" "[0-9]"

# Output
# remove  from this

5. Removing blank spaces

string_remove_pattern "remove space from this" "[[:space:]]"

# Output
# removespacefromthis

Inspired from Dylan Araps

Live Demo

Open Live Demo