How to Remove Special Characters
Quick Answer: Remove Special Characters from a String
To remove special characters from a string in Bash, use the tr command: echo "$text" | tr -cd '[:alnum:]'. This keeps only letters and numbers, stripping everything else. For more control over what to keep, use sed or parameter expansion with regex.
Quick Comparison: Special Character Removal Methods
| Method | Speed | Best For | Complexity |
|---|---|---|---|
| tr -cd | Very fast | Keep specific character sets | Simple |
| sed regex | Fast | Pattern-based removal | Moderate |
| Parameter expansion | Very fast | Simple patterns | Moderate |
| grep -o | Fast | Extracting matches | Simple |
| awk gsub | Fast | Complex transformations | Moderate |
Bottom line: Use tr for simple character set filtering, use sed for pattern-based removal.
Removing Special Characters from Strings
Sometimes you need to clean strings by removing special characters, keeping only letters, numbers, or specific patterns. This is useful for sanitizing user input, creating safe filenames, or processing data.
Using tr Command
The simplest approach with tr:
#!/bin/bash
text="Hello-World_123!"
# Keep only alphanumeric
clean=$(echo "$text" | tr -cd '[:alnum:]')
echo "$clean" # Output: HelloWorld123
Common tr Patterns
#!/bin/bash
text="Price: $99.99!"
# Remove punctuation
clean=$(echo "$text" | tr -d '[:punct:]')
echo "$clean" # Output: Price 9999
# Remove all non-alphanumeric
clean=$(echo "$text" | tr -cd '[:alnum:] ')
echo "$clean" # Output: Price 9999
# Remove all non-digits
digits=$(echo "$text" | tr -cd '[:digit:]')
echo "$digits" # Output: 9999
Using sed
More powerful pattern removal:
#!/bin/bash
text="hello@world#bash!"
# Remove all special chars except spaces
clean=$(echo "$text" | sed 's/[^a-zA-Z0-9 ]//g')
echo "$clean" # Output: helloworld bash
Practical Example: Sanitize Filename
#!/bin/bash
sanitize_filename() {
local filename="$1"
# Keep only alphanumeric, dash, underscore, dot
local clean=$(echo "$filename" | tr -cd '[:alnum:]._-')
echo "$clean"
}
result=$(sanitize_filename "My Document (2026).pdf")
echo "Safe filename: $result" # Output: MyDocument2026.pdf
Real-World Example: Clean User Input
#!/bin/bash
# Remove special chars from username input
clean_username() {
local input="$1"
# Keep only lowercase letters and numbers
echo "$input" | tr '[:upper:]' '[:lower:]' | tr -cd '[:alnum:]_'
}
username=$(clean_username "John_Doe@123")
echo "Username: $username" # Output: john_doe123
Bash Parameter Expansion Method
Pure Bash without external commands:
#!/bin/bash
remove_special() {
local text="$1"
local clean=""
for ((i=0; i<${#text}; i++)); do
char="${text:$i:1}"
# Keep only alphanumeric
if [[ "$char" =~ [[:alnum:]] ]]; then
clean+="$char"
fi
done
echo "$clean"
}
result=$(remove_special "Hello@World!")
echo "$result" # Output: HelloWorld
Comparison of Methods
text="test@123!"
# tr method (fast)
echo "$text" | tr -cd '[:alnum:]'
# sed method (more flexible)
echo "$text" | sed 's/[^a-zA-Z0-9]//g'
# Bash loop (no external tools, slower)
Important Notes
tr -ddeletes characterstr -cdkeeps only specified characters[:alnum:]includes letters and digits[:punct:]includes punctuation marks- Use
[^...]in regex to negate patterns
Summary
Use tr for fast character removal, sed for complex patterns, or pure Bash loops when no external commands are available. The tr -cd pattern is most efficient for keeping only safe characters.