在 shell 脚本中将一个子字符串替换为另一个字符串

Replace one substring to another string in shell script

#!/bin/bash 
filename="test.txt"
firstString="Suzi" 
secondString="I love Sara  & Marry"
 # do something...

#The result must be like this:
resultString=I love Sara  & Marry

ng="我爱萨拉和玛丽"

temp=$(printf '%s\n' "$secondString" | sed -e 's/[/&]/\&/g')

sed -i "s|$firstString|$temp|g" $文件名

使用上面的命令来搜索和替换文件中的字符串,对我有用。