diff --git a/README.md b/README.md index 3d9ee29..9efdbe4 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Bash-Preexec ============ -**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). +**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). Bashhub Logo diff --git a/bash-preexec.sh b/bash-preexec.sh index 5b3a3cd..d455041 100644 --- a/bash-preexec.sh +++ b/bash-preexec.sh @@ -41,6 +41,12 @@ if [ -z "${BASH_VERSION-}" ]; then return 1; fi +# We only support Bash 3.1+. +# Note: BASH_VERSINFO is first available in Bash-2.0. +if [[ -z "${BASH_VERSINFO-}" || BASH_VERSINFO[0] -lt 3 || BASH_VERSINFO[0] -eq 3 && BASH_VERSINFO[1] -lt 1 ]]; then + return 1 +fi + # Avoid duplicate inclusion if [[ -n "${bash_preexec_imported:-}" ]]; then return 0 diff --git a/test/bash-preexec.bats b/test/bash-preexec.bats index 69d5811..6f14ad4 100644 --- a/test/bash-preexec.bats +++ b/test/bash-preexec.bats @@ -39,6 +39,16 @@ set_exit_code_and_run_precmd() { [ -z "$output" ] } +@test "sourcing bash-preexec should exit with 1 if we're using an older version of bash" { + if type -p bash-3.0 &>/dev/null; then + run bash-3.0 -c "source \"${BATS_TEST_DIRNAME}/../bash-preexec.sh\"" + [ "$status" -eq 1 ] + [ -z "$output" ] + else + skip + fi +} + @test "__bp_install should exit if it's already installed" { bp_install