magixx
magixx

Reputation: 115

What is the proper quoting to assign an interpolated sqlite3 query to a variable in Bash?

I am trying to assign the output of the following to a variable

sqlite3 /home/user/db_fake_book_index "select id, page from fb2 where bookcode='$bookcode' and page=$page

If I run quote it like

echo "sqlite3 /home/user/db_fake_book_index \"select id, page from fb2 where bookcode='$bookcode' and page=$page\""

I get

sqlite3 /home/user/db_fake_book_index "select id, page from fb2 where bookcode='557S' and page=10"

and if I copy it to a console it works fine, but if i encase it in $() in my script it does not work, sqlite believes there are too many variables, it seems my double quotes are being removed.

$(printf "sqlite3 /home/user/db_fake_book_index \"select id, page from fb2 where bookcode='$bookcode' and page=$page\"")

This gives the same faliure

Upvotes: 1

Views: 1034

Answers (2)

chepner
chepner

Reputation: 531938

result=$( sqlite3 /home/user/db_fake_book_index "select id, page from fb2 where bookcode='$bookcode' and page=$page" )

or

query="select id, page from fb2 where bookcode='$bookcode' and page=$page"
result=$( sqlite3 /home/user/db_fake_book_index "$query" )

Upvotes: 3

Zombo
Zombo

Reputation: 1

This will save as the variable v

set "select id, page from fb2 where bookcode='$bookcode' and page=$page"
read v < <(sqlite3 /home/user/db_fake_book_index "$1")

Upvotes: 0

Related Questions