Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not enable bash-preexec.sh in non-interactive shells #160

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bash-preexec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/bash-preexec.bats
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions test/include-test.bats
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Loading