single quotes should be used to avoid expansion, e.g.
echo 'whatever ad!' | tr -d '[:punct:]'
under a bash shell it prints out
whatever ad
and if you want to use a variable
BUFF='whatever ?_-!!!!ad!'; echo "$BUFF" | tr -d '[:punct:]'
EDIT 1
this is a complete script following your request
#!/bin/sh
functionStripAndPrint()
{
echo "$@" | tr -d '[:punct:]'
}
functionStripAndPrint "$@"
assuming that this script is stored in the stripchars.sh
file, you can invoke it like so
./stripchars.sh 'das !adsa _sda ssad-'
and it will print
das adsa sda ssad
EDIT 2
you can work around the interpretation of some of the special characters with set
, for example
set +H
deactivates the H
option which is linked to the !
symbol, so now !
is just an exclamation mark with no special meaning. You can then simplify your invocation a little bit
./stripchars.sh sdfsa!fdsaf?\'
as you can see the only problem at this point is the '
that still needs to be escaped.
If you want to re-enable the H
you do
set -H
set
is handy to modify the behaviour of your shell, I don't know if it's worth in your case, the shell is good and handy for some basic stuff, but I don't know if this will fit your needs, you know better, take a look at set
and see if it's enough.