我有一个包含单引号(')的长字符串。我需要将其发送到mysql,因此在INSERT语句执行期间,我会尝试将变量中的"'“替换为"\'”。
示例:
"This is the first single quote ', and this is the second '"应该变成
"This is the first single quote \', and this is the second \'"我已经看过几个使用脚本替换和sed的示例,但无法理解这一个。感谢任何人的帮助。
谢谢
发布于 2014-08-03 15:51:02
您可以使用sed "s/'/\\\'/g"
echo "This is the first single quote ', and this is the second '" | sed "s/'/\\\'/g"发布于 2014-08-03 16:03:02
使用纯BASH字符串操作:
s="This is the first single quote ', and this is the second '"
s="${s//\'/\'}"
echo "$s"
This is the first single quote \', and this is the second \'https://stackoverflow.com/questions/25102648
复制相似问题