Skip to content

Commit da3f788

Browse files
authored
Fix #64 by quoting functions and set IFS on read (#66)
- Fixes bug where IFS in outer environment could be set to something that would cause our inner functions to break on varibale expansion. - Added a couple unit tests.
1 parent e81e7bf commit da3f788

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

bash-preexec.sh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ __bp_precmd_invoke_cmd() {
106106
# Test existence of functions with: declare -[Ff]
107107
if type -t "$precmd_function" 1>/dev/null; then
108108
__bp_set_ret_value "$__bp_last_ret_value" "$__bp_last_argument_prev_command"
109-
$precmd_function
109+
"$precmd_function"
110110
fi
111111
done
112112
}
@@ -189,7 +189,7 @@ __bp_preexec_invoke_exec() {
189189
fi
190190

191191
local this_command
192-
this_command=$(HISTTIMEFORMAT= builtin history 1 | { read -r _ this_command; echo "$this_command"; })
192+
this_command=$(HISTTIMEFORMAT= builtin history 1 | { IFS=" " read -r _ this_command; echo "$this_command"; })
193193

194194
# Sanity check to make sure we have something to invoke our function with.
195195
if [[ -z "$this_command" ]]; then
@@ -210,7 +210,7 @@ __bp_preexec_invoke_exec() {
210210
# Test existence of function with: declare -[fF]
211211
if type -t "$preexec_function" 1>/dev/null; then
212212
__bp_set_ret_value $__bp_last_ret_value
213-
$preexec_function "$this_command"
213+
"$preexec_function" "$this_command"
214214
preexec_function_ret_value="$?"
215215
if [[ "$preexec_function_ret_value" != 0 ]]; then
216216
preexec_ret_value="$preexec_function_ret_value"

test/bash-preexec.bats

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ test_preexec_echo() {
162162
[[ "${lines[1]}" == "two" ]]
163163
}
164164

165+
@test "preexec should execute a function with IFS defined to local scope" {
166+
IFS=_
167+
name_with_underscores_1() { parts=(1_2); echo $parts; }
168+
preexec_functions+=(name_with_underscores_1)
169+
170+
__bp_interactive_mode
171+
run '__bp_preexec_invoke_exec'
172+
[[ $status == 0 ]]
173+
[[ "$output" == "1 2" ]]
174+
}
175+
176+
@test "precmd should execute a function with IFS defined to local scope" {
177+
IFS=_
178+
name_with_underscores_2() { parts=(2_2); echo $parts; }
179+
precmd_functions+=(name_with_underscores_2)
180+
run '__bp_precmd_invoke_cmd'
181+
[[ $status == 0 ]]
182+
[[ "$output" == "2 2" ]]
183+
}
184+
165185
@test "preexec should set \$? to be the exit code of preexec_functions" {
166186
return_nonzero() {
167187
return 1

0 commit comments

Comments
 (0)