3 ms·
Yes! If you want to maintain a full history (and you should!), you can make bash consistently log it: promptFunc() { # right before prompting for t
by jefftk 5y ago
Yes! If you want to maintain a full history (and you should!), you can make bash consistently log it:
promptFunc() {
# right before prompting for the next command, save the previous
# command in a file.
echo "$(date +%Y-%m-%d--%H-%M-%S) $(hostname) $PWD $(history 1)" \
>> ~/.full_history
}
PROMPT_COMMAND=promptFunc
- pabs3 5y agoInstead of using that echo, you can tell bash to append the history: history -a Setting the histappend option does the append thing on exit: shopt -s histappend Instead of the ~/.full_history file you can just set your normal history to infinite: HISTFILESIZE=-1 HISTSIZE=-1
- jefftk 5y agoUnfortunately, with this approach it only takes one invocation of bash without your .bashrc (or otherwise without HISTFILESIZE=-1) to truncate your history to 500 lines: $ man bash /HISTFILESIZE The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, by removing the oldest entries, to contain no more than that number of lines. The default value is 500. The history file is also truncated to this size after writing it when an interactive shell exits. (I also like that with my approach I also get the timestamp and current direct rate.)
- pabs3 5y agoIt is for this reason that I also keep my bash history in git. I know other folks that do this too. There have also been solutions on HN in recent months for keeping your shell history in an sqlite database.
- _0w8t 5y agoThis will write duplicated entries to the file. Do you remove them on shell startup?
- jefftk 5y agoSince shell commands aren't necessarily (or even usually) idempotent, if I'm trying to figure out what I did earlier duplicate entries are still good to have.