I'm replacing, from a feed title, all chars except letters and digits with a dash to use the result as a safe filename for any filesystem:$ t=Episodie 06: No hope of riding home (NEW) - Advanced grammar$ echo ${t//[^A-Za-z0-9]/-}Episodie-06--No-hope-of-riding-home--NEW----Advanced-grammarHowever I'd like to condense all repeating dashes with a single one like Episodie-06-No-hope-of-riding-home-NEW-Advanced-grammarI found I can achieve it using a two pass substitution:$ t=Episodie 06: No hope of riding home (NEW) - Advanced grammar$ tmp=${t//[^A-Za-z0-9]/-}$ echo ${tmp//--/-}Episodie-06-No-hope-of-riding-home-NEW--Advanced-grammarI thought I could do it in a single pass like:$ echo ${t//[^A-Za-z0-9]+/-}but it doesn't work.Any clue?Note: I don't want to go with sed or other tools
Bash string replace multiple chars with one
bash;shell;string
You need something more powerful than traditional shell wildcards. In bash, set the extglob option, which gives you access to regular expressions in glob patterns through an unusual syntax inherited from ksh.shopt -s extglobsanitized=${raw//+([^A-Za-z0-9])/-}