需要帮助的bash脚本替换单引号 [英] Need help replacing single quotes in bash script

查看:123
本文介绍了需要帮助的bash脚本替换单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图替换字符串中的引号在我的bash脚本。我有一个将被放置在一个MySQL查询有可能有一个单引号中有一个变量。我想更换任何 \\ ,所以我可以逃脱的任何报价,而不是搞砸了我的查询。

我想这是一个测试,但它不工作:

 文本=Bobby的测试
回声$ {文/#'/ \\\\\\'}

我究竟做错了或有甚至是更好的办法做到这一点,我还没有想过?我preFER不具备使用sed或任何东西为好。


解决方案

回声$ {//文\\'/ \\ '}

请确保您使用在外面的双引号,否则你将需要更多的转义(啊)。

i am trying to replace the quotes in a string in my bash script. i have a variable that will be placed in a mysql query that may possibly have a single quote in it. i want to replace any ' with \' so i can escape any quote and not screw up my query.

i tried this as a test but it doesn't work:

text="bobby's test"
echo ${text/#'/\\\'}

what am i doing wrong or is there even a better way to do this that i havent thought of? i prefer to not have to use sed or anything as well.

解决方案

echo "${text//\'/\'}"

Make sure you use the double quotes on the outside, or you are going to need more escaping (ugh).

这篇关于需要帮助的bash脚本替换单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆