sqlite3 stores command history in .sqlite_history, which is by default created in:


How can I change this location to somewhere else?

This is possible for example with mysql, where I can define environment variable


But I could not find any corresponding environment variable for sqlite3

export SQLITE_HISTFILE=/tmp/history 

has no effect. I found a post where somebody asks same question, but no useful answers are given.


Since Version 3.25.3 you can simply set SQLITE_HISTORY to change the history filename, like mattmc3 wrote. In the versions before, it was hardcoded in line 5576 in shell.c (version 3.14.1):

sqlite3_snprintf(nHistory, zHistory,"%s/.sqlite_history", zHome);

So, to change it, one option among the others mentioned in this thread is was to edit the source and recompile.

  • Be sure to check @mattmc3 answer as this is now possible. – John Hascall Oct 1 '19 at 20:49

To list some of the possible alternatives,

  1. make ~/.sqlite_history into a symbolic link to another file.

  2. simply run HOME=/tmp sqlite3 to have the program save the history in /tmp/.sqlite_history, though this assumes you don't need the real home directory inside the command environment.

  3. edit the binary and replace the string "%s/.sqlite_history", found by Hoov, by another string with the same number of bytes, eg "/tmp/sqlitehistory":

    sed < /usr/bin/sqlite3 's|%s/.sqlite_history|/tmp/sqlitehistory|' >/tmp/sqlite3
    cmp -l /usr/bin/sqlite3 /tmp/sqlite3 # check no extraneous differences
    chmod +x /tmp/sqlite3
  4. use the LD_PRELOAD shim I proposed in an another question to change one filename ("/tmp/adb.log" in that case) to another during an open() call.

  • Excellent and enlightening answer. I feel ignorant now. – Hoov Sep 2 '16 at 7:36
  • But in reality, when this situation arises, I usually prefer to follow your answer and rebuild from sources, unless this proves to be too tedious, with too many dependencies etc. – meuh Sep 2 '16 at 7:53
  • Yes, it think it's the most manageable solution, but your solution three could come in handy when you don't have the source. Saved it to a script. – Hoov Sep 2 '16 at 8:07
  • Note that in sqlite's case, setting $HOME will not work, since sqlite does not honour that environment variable: it uses only what getpwuid() returns (e.g. from /etc/passwd). – ayekat Aug 31 '17 at 5:58

As of Oct 10, 2018 it appears that a new variable, $SQLITE_HISTORY, was added to address this.

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.