Replace one character with another in Bash
Use inline shell string replacement. Example:
foo=" "# replace first blank onlybar=${foo/ /.}# replace all blanksbar=${foo// /.}
See http://tldp.org/LDP/abs/html/string-manipulation.html for more details.
In bash, you can do pattern replacement in a string with the ${VARIABLE//PATTERN/REPLACEMENT}
construct. Use just /
and not //
to replace only the first occurrence. The pattern is a wildcard pattern, like file globs.
string='foo bar qux'one="${string/ /.}" # sets one to 'foo.bar qux'all="${string// /.}" # sets all to 'foo.bar.qux'