Often you may want to use Bash to remove specific characters from a string.
There are two common ways to do so:
Method 1: Remove One Specific Character from String
echo "${my_string//a}"
This particular example will remove each occurrence of the character a from the string variable named my_string.
Method 2: Remove Several Specific Characters from String
echo "${my_string//[acr]}"
This particular example will remove each occurrence of the characters a, c and r from the string variable named my_string.
Note that both of these methods use a technique known as shell parameter expansion, which uses the following syntax:
- ${parameter/pattern/string}
When the value for string is null then matches of pattern are simply deleted.
The following example shows how to use each of these methods in practice.
Example 1: Remove One Specific Character from String in Bash
Suppose we have the following string stored in a variable named $my_string in Bash:
- Dallas Mavericks
Suppose that we would like to remove each occurrence of the character a from the string.
We can use the following syntax to do so:
my_string="Dallas Mavericks"
echo "${my_string//a}"
The following screenshot shows how to use this syntax in practice:

Notice that the output returns the following string:
- Dlls Mvericks
Each occurrence of the character a has been removed from the string, just as we specified.
Example 2: Remove Several Specific Characters from String in Bash
Once again, suppose we have the following string stored in a variable named $my_string in Bash:
- Dallas Mavericks
Suppose that we would like to remove each occurrence of the characters a, c and r from the string.
We can use the following syntax to do so:
my_string="Dallas Mavericks"
echo "${my_string//[acr]}"
The following screenshot shows how to use this syntax in practice:

Notice that the output returns the following string:
- Dlls Mveiks
Each occurrence of the characters a, c and r have been removed from the string.
Note: Feel free to include as many characters as you’d like within the brackets to remove even more characters from the string.
Related Tutorials
The following tutorials explain how to perform other common tasks in Bash:
Bash: How to Check if String Starts with Specific Value
Bash: How to Count Number of Words in String
Bash: How to Count Number of Characters in String
