Skip to content

how to avoid double-sourcing? #1443

@Rudxain

Description

@Rudxain

Please document this in the README.

I've been wondering why my Bash startup time is so slow on Termux compared to Debian, while using identical dotfiles.

Note

For reference: Debian doesn't source any completions at all, while Termux sources all (most?) of the completion files installed by the package.

Turns out I was double-sourcing 2 files. Here's an excerpt from my ~/.bashrc:

bc="${TERMUX__PREFIX:-/usr}/share/bash-completion"

[[ -f $bc/bash_completion ]] && \
	\. "$bc/bash_completion"

if [[ -f $bc/completions/git ]]; then
	\. "$bc/completions/git"
	__git_complete g __git_main
fi

unset bc

I commented-out that, and the startup-time went from 0.53s to 0.29s (both real), according to time bash -i -c exit.

So instead of checking if the rc is running on Termux or Debian, I decided to use a cross-platform approach:

bc="${TERMUX__PREFIX:-/usr}/share/bash-completion"

[[ -z ${BASH_COMPLETION_VERSINFO:-} && -f $bc/bash_completion ]] && \
	\. "$bc/bash_completion"

[[ $(type -t __git_complete) != function ]] && \
[[ -f $bc/completions/git ]] && \
	\. "$bc/completions/git"
__git_complete g __git_main

unset bc

I copied the BASH_COMPLETION_VERSINFO check from /etc/profile.d/bash_completion.sh, and since the var is not prefixed by _ I assume this is API-stable and not an implementation detail. However, I'm afraid type is not the proper way to check if Git completions are loaded

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions