In a bash script I have
my_query="\'select uid from lastlog;\'"
If I then write
sqlite test.db ${my_query}
I get the error
/usr/bin/sqlite3: Error: too many options: "uid"
What is the correct way to escape the quotes?
Taking your example and echo it clearly shows the issue.
$ my_query="\'select uid from lastlog;\'"
$ echo $my_query
\'select uid from lastlog;\'
As your variable is already quoted, there is no need to escape the nested single quotes.
$ my_query="'select uid from lastlog;'"
$ echo $my_query
'select uid from lastlog;'
Someone wrote the following answer:
my_query='select uid from lastlog;'
sqlite test.db "${my_query}"
but then deleted it almost immediately, but luckily I saw it and it works for me.