Skip to content

Commit d523550

Browse files
committed
Improve prior_trap processing
We use `trap` to bootstrap our installation function (__bp_install). We remove our code upon first execution but need to restore any preexisting trap calls. We previously used `sed` to process the trap string, but that had two downsides: 1. `sed` is an external command dependency. It needs to exist on the system, and we need to invoke it in a subshell (which has some runtime cost). 2. The regular expression pattern was imperfect and didn't handle trickier cases like `'` characters in the trap string: $ (trap "echo 'hello'" DEBUG; trap -p DEBUG) hello trap -- 'echo '\''hello'\''' DEBUG This change removes the dependency on `sed` by locally evaluating the trap string and extracting any prior trap. This works reliably because we control the format our trap string, which looks like this (with newlines expanded): __bp_trap_string="$(trap -p DEBUG)" trap - DEBUG __bp_install
1 parent e8e9024 commit d523550

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

bash-preexec.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -297,10 +297,8 @@ __bp_install() {
297297
trap '__bp_preexec_invoke_exec "$_"' DEBUG
298298

299299
# Preserve any prior DEBUG trap as a preexec function
300-
local prior_trap
301-
# we can't easily do this with variable expansion. Leaving as sed command.
302-
# shellcheck disable=SC2001
303-
prior_trap=$(sed "s/[^']*'\(.*\)'[^']*/\1/" <<<"${__bp_trap_string:-}")
300+
eval "local trap_argv=(${__bp_trap_string:-})"
301+
local prior_trap=${trap_argv[2]:-}
304302
unset __bp_trap_string
305303
if [[ -n "$prior_trap" ]]; then
306304
eval '__bp_original_debug_trap() {

0 commit comments

Comments
 (0)