0

I have following in bash script:

sqlite3 database.db "INSERT INTO tbl (col1,col2) VALUES ('\"$line\"','\"$stdout\"');"

,where $line variable isn't going to make any trouble, but my $stdout variable is output from whois $line, so it contains many chars, one of them being ' which breaks the sqlite3 syntax.

Is there some way I could quote-escape that variable, so that it passes well in this query?

EDIT

Sorry, I found my solution here, https://stackoverflow.com/a/14340973/2434479.

Should I delete this question?

Community
  • 1
  • 1
branquito
  • 3,864
  • 5
  • 35
  • 60

1 Answers1

3

The standard SQL escape sequence for simple quote ' is the double simple quote ''.

Using bash pattern substitution, you may perform the necessary replacements:

sh$ DSQ="''"
sh$ stdout="I'm in a big'trouble"
sh$ echo "${stdout//\'/$DSQ}"
I''m in a big''trouble

In your specific case:

DSQ="''"
sqlite3 database.db \
    "INSERT INTO tbl (col1,col2) VALUES ('$line','${stdout//\'/$DSQ}');"
Sylvain Leroux
  • 50,096
  • 7
  • 103
  • 125