From 912f5aa0b55ddeea67c3aef04029e631f91b435a Mon Sep 17 00:00:00 2001 From: "Andrew R. M" Date: Fri, 4 Apr 2025 17:42:35 +0000 Subject: [PATCH] Improve bash history, add persistent history file --- bash/.bash_profile | 2 +- bash/.bashrc | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/bash/.bash_profile b/bash/.bash_profile index fd7e6f8..2cb9437 100644 --- a/bash/.bash_profile +++ b/bash/.bash_profile @@ -24,7 +24,7 @@ export HISTSIZE=-1 #{- SHELL OPTIONS -}# # Append history to $HISTFILE when bash exits, instead of overwriting it -shopt -s histappend +#shopt -s histappend # Attempt to preserve multi-line commands in history with embedded newlines shopt -s cmdhist lithist diff --git a/bash/.bashrc b/bash/.bashrc index 8529b48..be0a596 100644 --- a/bash/.bashrc +++ b/bash/.bashrc @@ -23,12 +23,31 @@ export PS4="${blue}#${no_color} " #{- HISTORY -}# -#shopt -s histappend -#export PROMPT_COMMAND="history -a; histroy -c; history -r; $PROMPT_COMMAND" +PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND" +# PROMPT_COMMAND="echo hist1; history; history -n; echo hist2; history; history -w; echo hist3; history; history -c; echo hist4; history; history -r; echo hist5; history; $PROMPT_COMMAND" +export HISTFILE="$HOME/.history/bash" +export HISTTIMEFORMAT="%F %T " +export HISTCONTROL="erasedups:ignoreboth" +export HISTFILESIZE=-1 +export HISTSIZE=-1 -export HISTCONTROL="ignoreboth:erasedups" -shopt -s histappend -export PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND" +export PERSISTENT_HISTFILE="${HOME}/.persistent_history" +log_bash_persistent_history() +{ + local rc=$? + [[ + $(history 1) =~ ^\ *[0-9]+\ +([^\ ]+\ [^\ ]+)\ +(.*)$ + ]] + local date_part="${BASH_REMATCH[1]}" + local command_part="${BASH_REMATCH[2]}" + if [ "$command_part" != "$PERSISTENT_HISTORY_LAST" ] + then + echo $date_part "|" "$rc" "|" "$command_part" >> "${PERSISTENT_HISTFILE}" + export PERSISTENT_HISTORY_LAST="$command_part" + fi +} + +PROMPT_COMMAND="log_bash_persistent_history; $PROMPT_COMMAND" #{- SOURCING -}#