I am trying to store user input in a variable and clean that variable in order to keep only alphanumerical caract + some others (I mean [a-zA-Z0-9-_]).
I tried using this but it isn’t exhaustive :
SERVICE_NAME=$(echo $SERVICE_NAME | tr A-Z a-z | tr ' ' _ | tr \' _ | tr \" _)
Do you have some help for this?
I would use
sedfor this and use the^(not) operator in your set of valid characters and replace everything else with an underscore. The above shows the syntax with the output.And, as a bonus, if you want to replace a run of invalid characters with one underscore, just add
+to your regular expression (and use the-rswitch tosedto make it use extended regular expressions: