I need to be able to do this via command line in one step:

lab-1:/etc/scripts# sqlite3 test.db
SQLite version 2015-05-20 18:17:19
Enter ".help" for usage hints.
sqlite> .mode csv ;
sqlite> .import /tmp/test.csv users
sqlite> select * from users;
sqlite> .quit

I've tried the following:

 lab-1:/etc/scripts# sqlite3 test.db ".mode csv ; .import /tmp/deleteme.csv users"


 lab-1:/etc/scripts# sqlite3 test.db ".mode csv .import /tmp/deleteme.csv users"

I don't get errors but I also don't end up with any data in the users table.

Any tips would be appreciated.


SQLite meta commands are not terminated by ; but by newline. Therefore, you will have to provide the commands in some other way so that newlines are inserted in the correct places.

Here's a few examples, of which I would probably use the first one (because it's readable).

  1. Use a here-document:

    sqlite3 testdb <<END_COMMANDS
    .mode csv
    .import /tmp/deleteme.csv users
  2. Format the commands with printf:

    printf '.mode csv\n.import /tmp/deleteme.csv users\n' | sqlite3 test.db
  3. Use a here-string with C-escapes (in shells that supports it):

    sqlite3 test.db <<<$'.mode csv\n.import /tmp/deleteme.csv users\n'
| improve this answer | |

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.