0

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?

0

2 Answers 2

1

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;'
Sign up to request clarification or add additional context in comments.

1 Comment

You are right about not needing to escape the nested quotes, but I still need some quotes for the call to sqlite to ensure that the whole query is interpreted as a single argument. This is shown in my own answer to the question (but which is in fact someone else's answer).
1

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.

1 Comment

You may find this tool helpful when you're writing shell scripts: shellcheck.net

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.