Created
May 1, 2018 23:38
-
-
Save goretkin/e25707fb845cfaa4fb54491948427b49 to your computer and use it in GitHub Desktop.
bash eternal history
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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