Search code examples
bashsystemd

Escape single quotes in shell invocation


I'm trying to write a systemd service file without resorting to using an external script.

I need to query an SQLite database and write the contents to a file. But my query uses double quotes. I need to wrap the query in single quotes and since systemd doesn't use a shell, I need to manually use one. So how do I accomplish this?

ExecStart=sh -c 'sqlite3 dbfile.db 'SELECT "The db value is: "||value FROM table' > output.log'

I have tried escaping the inner single quotes, but for some reason that doesn't work.


Solution

  • Try this:

    ExecStart=sh -c 'sqlite3 dbfile.db '\''SELECT "The db value is: "||value FROM table'\'' > output.log'
    

    I used to use MySQL and double quotes work as well. You can also give it a shot:

    ExecStart=sh -c 'sqlite3 dbfile.db "SELECT \"The db value is: \"||value FROM table" > output.log'