--- orig/shell-tools
+++ mod/shell-tools
@@ -45,8 +45,14 @@
 
     local traps=$(trap)
     if [ -n "${traps}" ]; then
+	# find existing actions associated with given condition, avoiding
+	# regex injection
 	local old_action=$(echo "${traps}" \
-	    | sed --quiet --expression="s/^trap[[:space:]]\+--[[:space:]]\+'\([^']\+\)'[[:space:]]\+${condition}\$/\1/p")
+	    | sed --quiet --expression="s/^trap[[:space:]]\+--[[:space:]]\+'\([^']\+\)'[[:space:]]\+\([^[:space:]]\+\)\$/C:\2\nA:\1/p" \
+	    | grep --after-context=1 --fixed-strings --line-regexp "C:${condition}" \
+	    | sed --quiet --expression='s/^A://p'
+)
+
 	action="${action}; ${old_action}"
     fi
     trap "${action}" "${condition}"
