Skip to content

Commit a131ff9

Browse files
authored
Check Bash version 3.1+ (#136)
1 parent e579058 commit a131ff9

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Bash-Preexec
55
============
66

7-
**preexec** and **precmd** hook functions for Bash in the style of Zsh. They aim to emulate the behavior [as described for Zsh](http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions).
7+
**preexec** and **precmd** hook functions for Bash 3.1+ in the style of Zsh. They aim to emulate the behavior [as described for Zsh](http://zsh.sourceforge.net/Doc/Release/Functions.html#Hook-Functions).
88

99
<a href="https://bashhub.com" target="_blank"><img src="https://bashhub.com/assets/images/bashhub-logo.png" alt="Bashhub Logo" width="200"></a>
1010

bash-preexec.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ if [ -z "${BASH_VERSION-}" ]; then
4141
return 1;
4242
fi
4343

44+
# We only support Bash 3.1+.
45+
# Note: BASH_VERSINFO is first available in Bash-2.0.
46+
if [[ -z "${BASH_VERSINFO-}" || BASH_VERSINFO[0] -lt 3 || BASH_VERSINFO[0] -eq 3 && BASH_VERSINFO[1] -lt 1 ]]; then
47+
return 1
48+
fi
49+
4450
# Avoid duplicate inclusion
4551
if [[ -n "${bash_preexec_imported:-}" ]]; then
4652
return 0

test/bash-preexec.bats

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,16 @@ set_exit_code_and_run_precmd() {
3939
[ -z "$output" ]
4040
}
4141

42+
@test "sourcing bash-preexec should exit with 1 if we're using an older version of bash" {
43+
if type -p bash-3.0 &>/dev/null; then
44+
run bash-3.0 -c "source \"${BATS_TEST_DIRNAME}/../bash-preexec.sh\""
45+
[ "$status" -eq 1 ]
46+
[ -z "$output" ]
47+
else
48+
skip
49+
fi
50+
}
51+
4252
@test "__bp_install should exit if it's already installed" {
4353
bp_install
4454

0 commit comments

Comments
 (0)