本文介绍了逃避shell变量单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个bash脚本插入值 sqlite的
数据库。该命令如下
I wrote a bash script to insert values to sqlite
database. The command is as follow
sqlite3 ${db_name} "insert into ${table_name} (${column1},${column2}) values ('$f1','$f2');"
此命令运行正常,直到 F1
变量包含一个单引号
This command works fine until f1
variable contains a single quote
# e.g f1="I'm just kidding"
# the command reported error
Error: near "m": syntax error
可能有人请告诉我如何才能逃脱变量中的单引号?
任何建议AP preciated。谢谢!
May someone please show me how can we escape the single quote inside the variable?Any recommendations are appreciated. Thanks!
推荐答案
要逃避SQL单引号,你双击它(的):
To escape a single quote for SQL, you double it (https://www.sqlite.org/faq.html#q14):
$ f1="I'm just kidding"
$ echo "${f1//\'/''}"
I''m just kidding
$ f2="no single quotes"
$ echo "${f2//\'/''}"
no single quotes
所以
sqlite3 ${db_name} "insert into ${table_name} (${column1},${column2}) values ('${f1//\'/''}','${f2//\'/''}');"
这篇关于逃避shell变量单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!