From 30e09cb48024c6ef027588757c33c6b23b3b64f8 Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Fri, 12 Jul 2024 01:45:08 +0900 Subject: [PATCH] Do not enable it in non-interactive shells --- bash-preexec.sh | 5 +++++ test/bash-preexec.bats | 6 ++++++ test/include-test.bats | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/bash-preexec.sh b/bash-preexec.sh index 641346a..75e8bb2 100644 --- a/bash-preexec.sh +++ b/bash-preexec.sh @@ -47,6 +47,11 @@ if [[ -z "${BASH_VERSINFO-}" ]] || (( BASH_VERSINFO[0] < 3 || (BASH_VERSINFO[0] return 1 fi +# We do not enable bash-preexc in non-interactive shells (except in tests) +if [[ $- != *i* && ! ${__bp_inside_test-} ]]; then + return 0 +fi + # Avoid duplicate inclusion if [[ -n "${bash_preexec_imported:-}" || -n "${__bp_imported:-}" ]]; then return 0 diff --git a/test/bash-preexec.bats b/test/bash-preexec.bats index 152d5a6..b8466eb 100644 --- a/test/bash-preexec.bats +++ b/test/bash-preexec.bats @@ -1,5 +1,11 @@ #!/usr/bin/env bats +# This variable tells bash-preexec.sh that it is loaded for the testing +# purposes. bash-preexec.sh is intended to be used in interactive shell +# sessions, so it is disabled in non-interactive shells by default. However, +# it still needs to be loaded in non-interactive shells for the Bats tests, +__bp_inside_test=yes + setup() { PROMPT_COMMAND='' # in case the invoking shell has set this history -s fake command # preexec requires there be some history diff --git a/test/include-test.bats b/test/include-test.bats index 36a5f25..bdb4bbc 100644 --- a/test/include-test.bats +++ b/test/include-test.bats @@ -1,5 +1,11 @@ #!/usr/bin/env bats +# This variable tells bash-preexec.sh that it is loaded for the testing +# purposes. bash-preexec.sh is intended to be used in interactive shell +# sessions, so it is disabled in non-interactive shells by default. However, +# it still needs to be loaded in non-interactive shells for the Bats tests, +__bp_inside_test=yes + @test "should not import if it's already defined" { bash_preexec_imported="defined" source "${BATS_TEST_DIRNAME}/../bash-preexec.sh"