Skip to content

Commit a2216fc

Browse files
committed
Make "bash_preexec_invoke_pre{cmd,exec}_functions" return the last non-zero exit status
1 parent dda9193 commit a2216fc

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

bash-preexec.sh

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,15 @@ __bp_precmd_invoke_cmd() {
151151

152152
# This function invokes every function defined in our function array
153153
# "precmd_function". This function receives the arguments $1 and $2 for $? and
154-
# $_, respectively, that will be set for the precmd functions.
154+
# $_, respectively, that will be set for the precmd functions. This function
155+
# returns the last non-zero exit status of the hook functions. If there is no
156+
# error, this function returns 0.
155157
bash_preexec_invoke_precmd_functions() {
156158
local lastexit=$1 lastarg=$2
157159
# Invoke every function defined in our function array.
158160
local precmd_function
161+
local precmd_function_ret_value
162+
local precmd_ret_value=0
159163
for precmd_function in "${precmd_functions[@]}"; do
160164

161165
# Only execute this function if it actually exists.
@@ -164,8 +168,13 @@ bash_preexec_invoke_precmd_functions() {
164168
__bp_set_ret_value "$lastexit" "$lastarg"
165169
# Quote our function invocation to prevent issues with IFS
166170
"$precmd_function"
171+
precmd_function_ret_value=$?
172+
if [[ "$precmd_function_ret_value" != 0 ]]; then
173+
precmd_ret_value="$precmd_function_ret_value"
174+
fi
167175
fi
168176
done
177+
return "$precmd_ret_value"
169178
}
170179

171180
# Sets a return value in $?. We may want to get access to the $? variable in our
@@ -254,8 +263,8 @@ __bp_preexec_invoke_exec() {
254263
return
255264
fi
256265

257-
local preexec_ret_value
258266
bash_preexec_invoke_preexec_functions "${__bp_last_ret_value:-}" "$__bp_last_argument_prev_command" "$this_command"
267+
local preexec_ret_value=$?
259268

260269
# Restore the last argument of the last executed command, and set the return
261270
# value of the DEBUG trap to be the return code of the last preexec function
@@ -270,14 +279,14 @@ __bp_preexec_invoke_exec() {
270279
# "preexec_function". This function receives the arguments $1 and $2 for $?
271280
# and $_, respectively, that will be set for the preexec functions. The third
272281
# argument $3 specifies the user command that is going to be executed
273-
# (corresponding to BASH_COMMAND in the DEBUG trap). This function assigns the
274-
# last non-zero exit status from the preexec functions to the variable
275-
# `preexec_ret_value`. If there is no error, preexec_ret_value is set to `0`.
282+
# (corresponding to BASH_COMMAND in the DEBUG trap). This function returns the
283+
# last non-zero exit status from the preexec functions. If there is no error,
284+
# this function returns `0`.
276285
bash_preexec_invoke_preexec_functions() {
277286
local lastexit=$1 lastarg=$2 this_command=$3
278287
local preexec_function
279288
local preexec_function_ret_value
280-
preexec_ret_value=0
289+
local preexec_ret_value=0
281290
for preexec_function in "${preexec_functions[@]:-}"; do
282291

283292
# Only execute each function if it actually exists.
@@ -292,6 +301,7 @@ bash_preexec_invoke_preexec_functions() {
292301
fi
293302
fi
294303
done
304+
return "$preexec_ret_value"
295305
}
296306

297307
__bp_install() {

0 commit comments

Comments
 (0)