1

I'd like to run series of sqlite commands in bash script:

db="main.sqlite3"
db2="sub.sqlite3"

sqlite3 ${db} <<EOF
attach ${db2} as m;
select count(*) from m.details;
.exit
EOF

when attach to a string, it works, but when it is a variable, it won't, got

Error: near line 1: near "/": syntax error

how to use a variable(db2) inside EOF? thanks

7
  • 2
    This should work as written. There is no / in the snippet you posted, so maybe you simplified it too much? Commented Aug 15, 2017 at 8:27
  • 1
    I suggest to check your file for special characters: cat -A file or cat -v file Commented Aug 15, 2017 at 8:27
  • 5
    Does the attach statement require the dbname to be quoted? Do you need attach '$db2' as m; ?? I can't tell from sqlite.org/syntax/attach-stmt.html and sqlite.org/syntax/expr.html Commented Aug 15, 2017 at 11:51
  • finally, that's the one works: sqlite3 ${db} <<EOF attach '${db2}' as m; select count(*) from m.details; .exit EOF Commented Aug 15, 2017 at 13:30
  • 1
    @jrwren No, because that question is about quoting "EOF"... Commented Aug 16, 2017 at 12:59

1 Answer 1

3

Try putting the DB file to attach into double quotes:

attach "${db2}" as m;
Sign up to request clarification or add additional context in comments.

Comments

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.