Skip to content

Instantly share code, notes, and snippets.

@nishanttotla
Last active January 26, 2017 23:11
Show Gist options
  • Save nishanttotla/c24e11cc67aa10c3703d548736aee9ad to your computer and use it in GitHub Desktop.
Save nishanttotla/c24e11cc67aa10c3703d548736aee9ad to your computer and use it in GitHub Desktop.
Persistent history across all bash terminals
# this part is used to record persistent bash history across terminals
# check http://eli.thegreenplace.net/2013/06/11/keeping-persistent-history-in-bash/ for details
HISTTIMEFORMAT="%d/%m/%y %T "
log_bash_persistent_history()
{
[[
$(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$
]]
local date_part="${BASH_REMATCH[1]}"
local command_part="${BASH_REMATCH[2]}"
# Uncomment the if statement to avoid repeatedly recording the same
# command when typed inside a single bash session. YMMV.
# if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ]
# this if statement is needed in case the above if statement isn't used
# because otherwise, pressing enter will create a duplicate entry
# for the last command that was input
if [ "$date_part" != "$PERSISTENT_HISTORY_LAST_MOMENT" ]
then
echo $date_part "|" "$command_part" "|" "$(pwd)" >> ~/.persistent_history
export PERSISTENT_HISTORY_LAST="$command_part"
export PERSISTENT_HISTORY_LAST_MOMENT="$date_part"
fi
}
# Stuff to do on PROMPT_COMMAND
run_on_prompt_command()
{
log_bash_persistent_history
}
PROMPT_COMMAND="run_on_prompt_command"
25/01/17 11:06:00 | set +x | /Users/myusername
25/01/17 11:06:01 | ls | /Users/myusername
25/01/17 11:09:03 | source ~/.bashrc | /Users/myusername
25/01/17 11:09:10 | source ~/.bashrc | /Users/myusername/work/src/github.com/docker/swarmkit
25/01/17 11:09:12 | source ~/.bashrc | /Users/myusername/work/src/github.com/docker/docker
25/01/17 11:09:13 | source ~/.bashrc | /Users/myusername
25/01/17 11:09:15 | source ~/.bashrc | /Users/myusername/Dropbox
25/01/17 11:09:59 | ls | /Users/myusername/Dropbox
25/01/17 11:10:03 | ls | /Users/myusername
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment