Skip to content

Commit

Permalink
Obtain existing DEBUG trap inside "__bp_install"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jan 13, 2025
1 parent a148f5a commit e644783
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ __bp_inside_precmd=0
__bp_inside_preexec=0

# Initial PROMPT_COMMAND string that is removed from PROMPT_COMMAND post __bp_install
__bp_install_string=$'__bp_trap_string="$(trap -p DEBUG)"\ntrap - DEBUG\n__bp_install'
__bp_install_string='__bp_install'

# Fails if any of the given variables are readonly
# Reference https://stackoverflow.com/a/4441178
Expand Down Expand Up @@ -326,14 +326,15 @@ __bp_install() {
return 1
fi

local trap_string
trap_string=$(trap -p DEBUG)
trap '__bp_preexec_invoke_exec "$_"' DEBUG

# Preserve any prior DEBUG trap as a preexec function
local prior_trap
# we can't easily do this with variable expansion. Leaving as sed command.
# shellcheck disable=SC2001
prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
unset __bp_trap_string
prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${trap_string:-}")
if [[ -n "$prior_trap" ]]; then
eval '__bp_original_debug_trap() {
'"$prior_trap"'
Expand Down Expand Up @@ -388,6 +389,11 @@ __bp_install() {
__bp_interactive_mode
}

# Note: We need to add "trace" attribute to the function so that "trap
# ... DEBUG" inside "__bp_install" takes an effect even when there was an
# existing DEBUG trap.
declare -ft __bp_install

# Sets an installation string as part of our PROMPT_COMMAND to install
# after our session has started. This allows bash-preexec to be included
# at any point in our bash profile.
Expand Down

0 comments on commit e644783

Please sign in to comment.