How to replace multiple spaces with a single space in a string with only bash built-ins

  • A+

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.

Here is a way to do this using pure bash and extglob:

s="too         many       spaces."  shopt -s extglob echo "${s//+([[:blank:]])/ }" 

too many spaces. 
  • Bracket expression [[:blank:]] matches a space or tab character
  • +([[:blank:]]) matches one or more of the bracket expression (requires extglob)


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: