I'd like to use bash to replace multiple adjacent spaces in a string by a single space. Example:
Original string:
"too many spaces."
Transformed string:
"too many spaces."
I've tried things like "${str//*( )/.}" or awk '{gsub(/[:blank:]/," ")}1' but I can't get it right.
Note: I was able to make it work with <CMD_THAT_GENERATES_THE_INPUT_STRINGH> | perl -lpe's/\s+/ /g' but I had to use perl to do the job. I'd like to use some bash internal syntax instead of calling an external program, if that is possible.
sedor any other external tool solution.str="too many spaces.";shopt -s extglob; printf '%s\n' "${str//+([[:space:]])/ }"echo $string | perlnote: When IFS is at its default value,echo $stringitself will replace runs of multiple spaces with a single space (along with other side effects, like replacing a wildcard in that string with a list of files) -- one needs to useecho "$string"to keep them in. (Even then,echocan still munge contents rather than emitting them exactly as they exist;printf '%s\n' "$string"is much more reliable).