diff --git a/bashhub/shell/deps/bash-preexec.sh b/bashhub/shell/deps/bash-preexec.sh index d93cb9a..8ca2bcf 100644 --- a/bashhub/shell/deps/bash-preexec.sh +++ b/bashhub/shell/deps/bash-preexec.sh @@ -11,7 +11,7 @@ # Author: Ryan Caloras (ryan@bashhub.com) # Forked from Original Author: Glyph Lefkowitz # -# V0.3.1 +# V0.3.2 # # General Usage: @@ -43,10 +43,11 @@ __bp_imported="defined" # Should be available to each precmd and preexec # functions, should they want it. __bp_last_ret_value="$?" +__bp_last_argument_prev_command="$_" # Command to set our preexec trap. It's invoked once via # PROMPT_COMMAND and then removed. -__bp_trap_install_string="trap '__bp_preexec_invoke_exec' DEBUG;" +__bp_trap_install_string="trap '__bp_preexec_invoke_exec \"\$_\"' DEBUG;" # Remove ignorespace and or replace ignoreboth from HISTCONTROL # so we can accurately invoke preexec with a command from our @@ -97,7 +98,7 @@ __bp_precmd_invoke_cmd() { # Only execute this function if it actually exists. # Test existence of functions with: declare -[Ff] if type -t "$precmd_function" 1>/dev/null; then - __bp_set_ret_value $__bp_last_ret_value + __bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command" $precmd_function fi done @@ -137,6 +138,11 @@ __bp_in_prompt_command() { # interactively, and invoke 'preexec' if so. __bp_preexec_invoke_exec() { + + # Save the contents of $_ so that it can be restored later on. + # https://stackoverflow.com/questions/40944532/bash-preserve-in-a-debug-trap#40944702 + __bp_last_argument_prev_command="$1" + # Checks if the file descriptor is not standard out (i.e. '1') # __bp_delay_install checks if we're in test. Needed for bats to run. # Prevents preexec from being invoked for functions in PS1 @@ -194,6 +200,9 @@ __bp_preexec_invoke_exec() { $preexec_function "$this_command" fi done + + # Restore the last argument of the last executed command + : "$__bp_last_argument_prev_command" } # Returns PROMPT_COMMAND with a semicolon appended @@ -253,7 +262,7 @@ __bp_install() { # Install our hooks in PROMPT_COMMAND to allow our trap to know when we've # actually entered something. PROMPT_COMMAND="__bp_precmd_invoke_cmd; ${existing_prompt_command} __bp_interactive_mode;" - trap '__bp_preexec_invoke_exec' DEBUG; + eval "$__bp_trap_install_string" # Add two functions to our arrays for convenience # of definition.