Skip to content

Commit e644783

Browse files
committed
Obtain existing DEBUG trap inside "__bp_install"
1 parent a148f5a commit e644783

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

bash-preexec.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ __bp_inside_precmd=0
7171
__bp_inside_preexec=0
7272

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

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

329+
local trap_string
330+
trap_string=$(trap -p DEBUG)
329331
trap '__bp_preexec_invoke_exec "$_"' DEBUG
330332

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

392+
# Note: We need to add "trace" attribute to the function so that "trap
393+
# ... DEBUG" inside "__bp_install" takes an effect even when there was an
394+
# existing DEBUG trap.
395+
declare -ft __bp_install
396+
391397
# Sets an installation string as part of our PROMPT_COMMAND to install
392398
# after our session has started. This allows bash-preexec to be included
393399
# at any point in our bash profile.

0 commit comments

Comments
 (0)