hjr265, 7 months ago (edited 7 months ago) @adamsdesk I don't think the pattern you pass there can be a regular expression. Bash has its own pattern matching rules. https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html I think this does what you are trying to do (replace 2 or more consecutive dashes with a single dash): string="Hello-World! #debug command---line" string="${string//-+(-)/-}" But this will work only if you enable extglob first: shopt -s extglob Alternatively, use sed: string=`echo "$string" | sed -e 's/--\+/-/g'`
@adamsdesk I don't think the pattern you pass there can be a regular expression. Bash has its own pattern matching rules.
https://www.gnu.org/software/bash/manual/html_node/Pattern-Matching.html
I think this does what you are trying to do (replace 2 or more consecutive dashes with a single dash):
string="Hello-World! #debug command---line" string="${string//-+(-)/-}"
But this will work only if you enable extglob first:
shopt -s extglob
Alternatively, use sed:
string=`echo "$string" | sed -e 's/--\+/-/g'`