The following script

QUERY='select * from cdr;'
MYROWS=$("sqlite3 -list -nullvalue NULL -separator ',' /var/log/asterisk/master.db '${QUERY}'")

gives me

./bla.sh: row 35: sqlite3 -list -nullvalue NULL -separator ',' /var/log/asterisk/master.db 'select * from cdr;': file or directory not found

If I execute directly

sqlite3 -list -nullvalue NULL -separator ',' /var/log/asterisk/master.db 'select * from cdr;'

then it works. I guess there is some error with the quotes that can't be seen on the error message. Please note that I need the single quotes around

select * from cdr;

Thanks for any hint on what I am doing wrong!

  • 1
    In the second line use double quotes only in the last part. Like: MYROWS=$(sqlite3 ... "${QUERY}"). You don't need single quotes because you have captured the special characters in the previous line. – forcefsck Sep 9 '12 at 21:37
  • Thanks, that works, but I don't really understand way, because echo "${QUERY}" gives the query without the ' and ', so why does it work in the MYROWS line? – stefan.at.wpf Sep 9 '12 at 22:37
  • 2
    All you need is to pass the string select * from cdr; as a parameter to sqlite3. You need to use quotes (single or double) so the shell will pass it as one parameter, not because sqlite3 requires it. Because you need ${QUERY} to be expanded, you have to use double quotes in this case. – forcefsck Sep 10 '12 at 14:31

What's inside $(…) is a shell command with the usual syntax. The double quotes around the whole snippet you intend as a command make it parsed as a single word, which is the first word of the command so it's interpreted as a command name.

Furthermore, your quoting of $QUERY isn't right: you need double quotes around it, so that the variable is expanded.

MYROWS=$(sqlite3 -list -nullvalue NULL -separator ',' /var/log/asterisk/master.db "${QUERY}")
  • I understand the first part of your reply, but not the one about explaing ${QUERY}, because when one simpley does echo ${QUERY}, therefore without any quotes, it is also expanded. I guess the shell is packaging the parameters into some kind of list and for the query to be one parameter, I need the quotes around it? But then, why do I need double quotes? Why not just single quotes? Thanks :-) – stefan.at.wpf Sep 10 '12 at 14:29
  • Found the difference between ' and " on linuxcommand.org/wss0060.php. Together with forcefsck's comment I now understand it completely :-) – stefan.at.wpf Sep 10 '12 at 14:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.