I would like to replace blank spaces/white spaces in a string with commas.
STR1=This is a string to
STR1=This,is,a,string Without using external tools:
echo ${STR1// /,} Demo:
$ STR1="This is a string" $ echo ${STR1// /,} This,is,a,string awk)?extglob on, you can do something like ${STR1//+( )/,}.kent$ echo "STR1=This is a string"|awk -v OFS="," '$1=$1' STR1=This,is,a,string Note:
if there are continued blanks, they would be replaced with a single comma. as example above shows.
How about
STR1="This is a string" StrFix="$( echo "$STR1" | sed 's/[[:space:]]/,/g')" echo "$StrFix" **output** This,is,a,string If you have multiple adjacent spaces in your string and what to reduce them to just 1 comma, then change the sed to
STR1="This is a string" StrFix="$( echo "$STR1" | sed 's/[[:space:]][[:space:]]*/,/g')" echo "$StrFix" **output** This,is,a,string I'm using a non-standard sed, and so have used ``[[:space:]][[:space:]]*to indicate one or more "white-space" characters (including tabs, VT, maybe a few others). In a modern sed, I would expect[[:space:]]+` to work as well.