3

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

$HOME/.sqlite_history

How can I change this location to somewhere else?

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

MYSQL_HISTFILE=/path/to/whatever/file 

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.

4

It looks like it's hardcoded in line 5576 in shell.c (version 3.14.1):

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

So, to change it, your only option is to edit the source and recompile.

2

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
    /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
1

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.