Remove special characters from string

Hi there,
I'd like to write a script that removes any set of character from any string. The first argument would be the string, the second argument would be the characters to remove. For example:

$ myscript "My name's Santiago. What's yours?" "atu"
My nme's Snigo. Wh's yors?

I wrote the following code:

$ cat myscript
ans=$1
for (( i=0; i<${#2}; i++ )); do
    ans=${ans//${2:$i:1}/}
done
echo "$ans"

But it doesn't work with the following special characters: #%*?\
Is there any simplier way to do that?
How can I make it work with all the characters?

Thanks in advance
Santiago

Try passing the special characters with a \ in front.
On the other hand why not simply use regular expressions with sed or perl etc. ?

Or simply with tr. Check the -d option in the man page.

Regards

That's exactly what I was asking! I'm sure there's a simpler way of doing that... But how?
I cannot pass the special characters with a backslash because my script reads the second argument character by character. So if $2 is "\#", the script will first try to replace \ (and it fails) then try to replace # (and it fails too).
Any other idea?

for some reason its working with single quotes in the command line arguments. I dont know why this is though *shrugs*

And what's wrong with Franklin52's solution?

This is with bash:

$ ./s "My name's Santiago. What's yours?" 'atu'
My nme's Snigo. Wh's yors?
$ ./s "My# name%'s ?Santiago.\ What/'s yours?" '#%?/\'
My name's Santiago. What's yours
$ cat s
#! /bin/bash

tr 2>&- -d "$2"<<<"$1" 

Great! The only character that do not work is backslah but here my new solution:

~$ cat myscript
echo "$1" | tr -d "${2//\\/\\\\}"
~$ myscript 'weird string !"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~' ' !"#$%&'\''()*+,-./:;<=>?@[\]^_`{|}~'
            <---------------------- $1 ----------------------> <---------------- $2 ---------------->
weirdstring
~$

Thanks a lot

Absolutely nothing, I was working on it while you posted this solution.
It looks great exept that as a newbie, I don't fully understand the syntax

What is <<<? Is this a way of doing echo?
I understand the tr -d but what does 2>&- means?
redirect errors to &-? What is &-?
Thanks for your help.
Santiago

Here string. It's not available in all shells.
For more information search your shell manpages for Here Strings.

2>&- closes the file descriptor 2. This is a bad practice and you should avoid it. My post was for illustration purposes only.

Hi

I have the following string which contains -> symbol, i need to split this string into two strings using shell script in unix. can anyone help me out.

test_inbox123_link.txt->/home/pbommire/Praveen/kiran/inbox/test_inbox.txt

r

Hi KiranKumarKarre,

localhost:~# string='test_inbox123_link.txt->/home/pbommire/Praveen/kiran/inbox/test_inbox.txt'
localhost:~# echo ${string%->*}
test_inbox123_link.txt
localhost:~# echo ${string#*->}
/home/pbommire/Praveen/kiran/inbox/test_inbox.txt
echo "My name's Santiago. What's yours?" | tr -d "atu"