jlp jlp - 11 months ago 63
Bash Question

How to remove special characters from strings but keep underscores in shell script

I have a string that is something like "info_A!__B????????C_*". I wan to remove the special characters from it but keep underscores and letters. I tried with [:word:] (ASCII letters and _) character set, but it says "invalid character set". any idea how to handle this ? Thanks.

if [ -z `echo $text | tr -dc "[:word:]"` ]


Using bash parameter expansion:

$ var='info_A!__B????????C_*'

$ echo "${var//[^[:alnum:]_]/}"