From 6b7a9bef566f0b533926fe54a7e88d19b46d10bf Mon Sep 17 00:00:00 2001 From: Koichi Murase Date: Fri, 12 Jul 2024 01:45:08 +0900 Subject: [PATCH] Do not enable bash-preexec in non-interactive shells --- bash-preexec.sh | 6 ++++++ test/bash-preexec.bats | 6 ++++++ test/include-test.bats | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/bash-preexec.sh b/bash-preexec.sh index 641346a..cd7bee5 100644 --- a/bash-preexec.sh +++ b/bash-preexec.sh @@ -47,6 +47,12 @@ 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). +# `__bp_inside_test` is set in test/*.bats in bash-preexec repository. +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..c1593ea 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 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..7d2bc38 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 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"