Skip to content

Instantly share code, notes, and snippets.

@wwalker
Last active October 18, 2019 00:14
Show Gist options
  • Save wwalker/c0ecd0b22db4f61ee290891d848b82ff to your computer and use it in GitHub Desktop.
Save wwalker/c0ecd0b22db4f61ee290891d848b82ff to your computer and use it in GitHub Desktop.
How to get bash per session history files, which is updated with each command in real time, and, upon session exit, appends the entire session history to ~/.bash_history
# Take the session history and append it to the .bash_history file, and if successful, remove the session file
cat "$HISTFILE" >> "$HOME"/.bash_history && /bin/rm "$HISTFILE"
# Store the time the session started
SESSION_START_TIME=$(date +%F_%H.%M.%S)
HISTCONTROL=
HISTSIZE=100000
HISTFILESIZE=100000
HISTTIMEFORMAT='%F %T - '
shopt -s histappend
shopt -s histreedit
shopt -s histverify
shopt -s cmdhist
HISTFILE="$HOME/.bash_history_$SESSION_START_TIME"
PROMPT_COMMAND="history -a;$PROMPT_COMMAND"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment