Table of Contents
Using Parameter Expansion
Use the parameter expansion with regex to remove the special characters from a string in Bash.
1 2 3 4 5 6 |
#!/bin/bash string="Hello World! This is a test string." new_string="${string//[^[:alnum:]]/""}" echo "The Modified String: $new_string" |
1 2 3 |
The Modified String: HelloWorldThisisateststring |
In this example, the parameter expansion removes all special characters from the "Hello World! This is a test string."
string. We can observe that our input string contained two special characters, the exclamation marks !
and the space character removed in the modified string.
Here’s a breakdown of the ${string//[^[:alnum:]]/""}
expression:
string
is the input string we want want to modify.//
is a pattern substitution operator that replaces all occurrences of a
pattern with an empty string.[^[:alnum:]]
[^[:alnum:]]
is a regular expression pattern that matches any character that is not alphanumeric. The^
symbol inside the square brackets meansnot
, so this pattern matches any character, not in the[:alnum:]
character class. The[:alnum:]
character class represents all alphanumeric characters (letters and digits).""
is an empty string used as the replacement string.
Using sed
Command
Use the sed
command to remove the special characters from a string in Bash.
1 2 3 4 5 6 |
#!/bin/bash string="Hi, it's John. How are you?" new_string=$(echo "$string" | sed 's/[^[:alnum:]]//g') echo "The Modified String: $new_string" |
1 2 3 |
The Modified String: HiitsJohnHowareyou |
This example used the sed
command to replace all non-alphanumeric characters with an empty string. The substitution operation(s
) is used to replace the pattern [^[:alnum:]]
with an empty string. Here, the //
indicates that we want to replace the pattern with an empty string globally, and g
is a flag that tells sed
to perform the substitution operation on all matches in the input string, not just the first match.
Using tr
Command
Use the tr
command to remove the special characters from a string in Bash.
1 2 3 4 5 6 |
#!/bin/bash new_string=$(echo "$string" | tr -dc '[:alnum:]') echo "The Modified String: $new_string" |
1 2 3 |
The Modified String: Myemailisabcgmailcom |
In the above example, the tr
command deleted all characters not alphanumeric from the "My email is [email protected]"
string. In other words, it is used to remove all special characters. Here, in the-dc
parameter, d
means delete and c
means character. This -dc
parameter tells the tr
command to delete all characters that do not match the specified character set [:alnum:]
(which matches all alphanumeric characters).
Using awk
Command
Use the awk
command to remove the special characters from a string in Bash.
1 2 3 4 5 6 |
#!/bin/bash string="The phone number of the company is :(555) 555-1234" new_string=$(echo "$string" | awk '{gsub(/[^[:alnum:]]/,"")}1') echo "The Modified String: $new_string" |
1 2 3 |
The Modified String: Thephonenumberofthecompanyis5555551234 |
In this bash example, the awk
command removes the special characters from the "The phone number of the company is :(555) 555-1234"
string and replaces all non-alphanumeric characters with an empty string. Here, the gsub
function of the awk
command is used to replace the pattern [^[:alnum:]]
with an empty string ""
, and 1
is used to print the modified string.
Using grep
Command
Use the grep
command to remove the special characters from a string in Bash.
1 2 3 4 5 6 |
#!/bin/bash string="The phone number of company is :(555) 555-1234" new_string=$(echo "$string" | grep -oE '[[:alnum:]]+' | tr -d '\n') echo "The Modified String: $new_string" |
1 2 3 |
The Modified String: Thephonenumberofcompanyis5555551234 |
The grep
command is used in the above example to match all alphanumeric characters in the string. The -o
parameter outputs only the matching pattern, and the -E
option enables extended regular expressions.
Here, the tr
command removes any newline characters from the output of $new_string
, effectively printing the output in a single line.
That’s all about bash remove special characters from String.