Replace special characters with backslash and character

Hi,

I have a string wherein i need to replace special characters with backslash and that character.
Ex:
If my string is

a=qwerty123@!

,
then the new string should be

a_new=qwerty123\@\!\

,

Thanks

depends on what you define as special.

This will escape any non-alphanum,hyphen or underscore:

$ echo 'qwerty123@!,' | sed 's/[^[:alnum:]_-]/\\&/g'
qwerty123\@\!\,
1 Like

By special i mean all characters except 0-9, A-Z and a-z.

What do you think you would need to do to the script Chubler_XL suggested to make it work with your new definition of special characters?