Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save goretkin/e25707fb845cfaa4fb54491948427b49 to your computer and use it in GitHub Desktop.
Save goretkin/e25707fb845cfaa4fb54491948427b49 to your computer and use it in GitHub Desktop.
bash eternal history
export HISTTIMEFORMAT="%s "
# this we need because we could be su'd to another user and we still want history here
chmod 666 ~/.bash_eternal_history
# reason for ~/../ is if su'ing into another user
HIST_FILE_ETERNAL=~/../goretkin/.bash_eternal_history
# add a header to indicate a new session
echo "#" $$, $USER, $(pwd), "parent:"$(ps -o ppid= -p $$), $(date), $(pwd) >> $HIST_FILE_ETERNAL
# This immediately writes the last line of the current session's history to a file
# keep track of the PID so that you can separate different terminal sessions
# this executes before the command in question, as opposed to a solution that
# modifies the $PROMPT_COMMAND variable.
# http://superuser.com/questions/175799/does-bash-have-a-hook-that-is-run-before-executing-a-command
preexec_invoke_exec () {
if [ -n "$COMP_LINE" ]; then
# echo "complete"
return
fi # do nothing if completing
if [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ]; then
# echo "prompt"
return
fi # don't cause a preexec for $PROMPT_COMMAND
echo $$ $USER "$(history 1)" >> $HIST_FILE_ETERNAL
}
trap 'preexec_invoke_exec' DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment