Followup-For: Bug #713051
Package: bash
Version: 5.3-3+b1

Though I'm using the latest stable, I inspected the contents of the
above package version from sid to confirm the same situation exists.

The bash_completion script will run twice by default in login shells,
like via SSH or inside tmux, though only once in others such as those
spawned by qterminal. This can be verified with a simple script at
/etc/bash_completion.d/test containing:

echo test sourced${_test_completion_loaded+' again'} >&2
_test_completion_loaded=yes

The problem is that /etc/bash.bashrc comments out the sourcing of the
completion script in the "if ! shopt -oq posix; then" block, then the
/etc/profile loads it anyway (via /etc/profile.d/bash_completion.sh).
The profile route is guarded against double inclusion but normally
this guard is unused, then the user's .bashrc (by default from
/etc/skel/.bashrc) uses the same unguarded code that /etc/bash.bashrc
contains, and so it's sourced a second time. I thought this could be
so users can opt out of completion by commenting out only their own
.bashrc's sourcing. However, this only works for shells that don't use
the profile route.

On Ubuntu, the commenting of the unguarded loads in /etc/bash.bashrc
and skel is reversed. So /etc/bash.bash does load completion, the
profile's guarded load short circuits, the skel file won't, and it
loads only once regardless of how the shell is invoked. This seems
like a better choice because the profile route including completion
(in both distros) implies it should be enabled system-wide.

Debian's wiki page (https://wiki.debian.org/Add%20Bash%20Completion)
recommends the /etc/bash_completion.d/ option for adding completions
to the whole system that aren't from packages, so anything in there
runs twice for every shell, but should really be lazy-loaded from
/usr/local/share/bash-completion/completions/

I agree that automatic load should be guarded against double-sourcing,
but manual re-sourcing should be possible, so the guard cannot be
within the bash_completion script itself. Loading via
/etc/profile.d/bash_completion.sh always, as Reuben suggsted or
swapping the comments would at least fix the default. The problem then
becomes how to migrate existing users that have an unguarded load in
their .bashrc.

Whatever the solution, the problem exists on an unmodified system.
Debian should not be running things twice nor recommending eager
loading.

Reply via email to