Skip to content
Advertisement

how to remove the special characters from a variable using shell

str="#$hello,)&^this I!s> m@ani: /& "

I need the perfect alphabetical string without special characters. can you suggest me how to do in shell

Advertisement

Answer

The following solution uses the tr command:

$ str=`echo '"#$hello,)&^this I!s> m@ani: /& "'`
$ echo $str | tr -cd "[:alnum:]"n"
"hellothisIsmani"

All letters and digits, all ” and new lines are allowed. If you want more or less characters to be allowed change the command.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement