From bc8bfe4aa952de195d93fea2adace88899080b0e Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 27 Jul 2021 18:44:22 -0700 Subject: [PATCH 1/2] Warn user if $HISTCONTROL is modified Warn the user on stderr if we need to modify `$HISTCONTROL`, but first check if our warning should be suppressed by the user setting `$__bp_suppress_histcontrol_warning` to something. --- bash-preexec.sh | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/bash-preexec.sh b/bash-preexec.sh index f9f6aa9..8eb2f86 100644 --- a/bash-preexec.sh +++ b/bash-preexec.sh @@ -80,7 +80,14 @@ __bp_adjust_histcontrol() { if [[ "$histcontrol" == *"ignoreboth"* ]]; then histcontrol="ignoredups:${histcontrol//ignoreboth}" fi; - export HISTCONTROL="$histcontrol" + + if [[ -z "${__bp_suppress_histcontrol_warning:-}" && "$HISTCONTROL" == *ignore+(dups|space)* ]] + then + echo "bash-preexec is unable to determine full command line when \$HISTCONTROL contains 'ignorespace' (or 'ignoreboth')." >&2 + echo "Those options have been removed from \$HISTCONTROL." >&2 + fi + + HISTCONTROL="$histcontrol" } # This variable describes whether we are currently in "interactive mode"; From 321d74e6e70c5ce9777db9f473a412015bd6a8f0 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Tue, 27 Jul 2021 18:36:23 -0700 Subject: [PATCH 2/2] Use $BASH_COMMAND if HISTCONTROL is incompatible When determining the command line, check if `$HISTCONTROL` is configured as expected. If not, then fall back to the built-in variable `$BASH_COMMAND` for a second-best command line. This accounts for when the user either changes `$HISTCONTROL` after loading `bash-preexec`, or otherwise removes/disables `__bp_adjust_histcontrol`. --- bash-preexec.sh | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/bash-preexec.sh b/bash-preexec.sh index 8eb2f86..e07c081 100644 --- a/bash-preexec.sh +++ b/bash-preexec.sh @@ -81,7 +81,7 @@ __bp_adjust_histcontrol() { histcontrol="ignoredups:${histcontrol//ignoreboth}" fi; - if [[ -z "${__bp_suppress_histcontrol_warning:-}" && "$HISTCONTROL" == *ignore+(dups|space)* ]] + if [[ -z "${__bp_suppress_histcontrol_warning:-}" && "$HISTCONTROL" == *ignore+(both|space)* ]] then echo "bash-preexec is unable to determine full command line when \$HISTCONTROL contains 'ignorespace' (or 'ignoreboth')." >&2 echo "Those options have been removed from \$HISTCONTROL." >&2 @@ -232,10 +232,12 @@ __bp_preexec_invoke_exec() { fi local this_command - this_command=$( - export LC_ALL=C - HISTTIMEFORMAT= builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //' - ) + if [[ "$HISTCONTROL" == *ignore+(both|space)* ]] + then + this_command="${BASH_COMMAND:-}" + else + this_command=$( LC_ALL=C HISTTIMEFORMAT= builtin history 1 | sed '1 s/^ *[0-9][0-9]*[* ] //' ) + fi # Sanity check to make sure we have something to invoke our function with. if [[ -z "$this_command" ]]; then