Replace one character with another in Bash

Brian Leishman picture Brian Leishman · May 8, 2011 · Viewed 240.1k times · Source

I need to be able to do is replace a space () with a dot (.) in a string in bash.

I think this would be pretty simple, but I'm new so I can't figure out how to modify a similar example for this use.

Answer

Brian Clapper picture Brian Clapper · May 8, 2011

Use inline shell string replacement. Example:

foo="  "

# replace first blank only
bar=${foo/ /.}

# replace all blanks
bar=${foo// /.}

See http://tldp.org/LDP/abs/html/string-manipulation.html for more details.