首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Bash脚本用单引号和反斜杠替换单引号

问Bash脚本用单引号和反斜杠替换单引号
EN

Stack Overflow用户
提问于 2014-08-03 15:40:57
回答 2查看 753关注 0票数 0

我有一个包含单引号(')的长字符串。我需要将其发送到mysql,因此在INSERT语句执行期间,我会尝试将变量中的"'“替换为"\'”。

示例:

代码语言:javascript
复制
"This is the first single quote ', and this is the second '"

应该变成

代码语言:javascript
复制
"This is the first single quote \', and this is the second \'"

我已经看过几个使用脚本替换和sed的示例,但无法理解这一个。感谢任何人的帮助。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2014-08-03 15:51:02

您可以使用sed "s/'/\\\'/g"

代码语言:javascript
复制
echo "This is the first single quote ', and this is the second '" | sed "s/'/\\\'/g"
票数 1
EN

Stack Overflow用户

发布于 2014-08-03 16:03:02

使用纯BASH字符串操作:

代码语言:javascript
复制
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 \'
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25102648

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档