var="abcdabe" echo ${var/ab/fg}
returns:
fgcdabe
NOTE: Only the first match is changed.
To change all occurrences use:
var="abcdabe" echo ${var//ab/fg}
returns:
fgcdfge
contents=$(< infile.txt) $ echo "${contents/$old/$new}"
NOTE: This reads the file into a Bash variable and uses parameter expansion.
To change the file in-place:
echo "${contents/$old/$new}" > infile.tmp && mv infile.tmp infile.txt