Does bash source bash completion files in /usr/local/etc/bash_completion.d by default?
Asked Answered
G

1

8

I have a bunch of bash completion files in

/usr/local/etc/bash_completion.d

most of the scripts in there have something like this at the bottom of them:

complete -F _tmux tmux

the above is for tmux.

My question is - it doesn't look like bash by default sources these files? I see some instructions online about doing something like this:

for f in '/usr/local/etc/bash_completion.d/'*; do
   source "$f"
done;

do I need to do this manually or should bash be doing this out of the box?

Glottis answered 31/5, 2019 at 5:14 Comment(4)
Are you on MacOS. Aren't the completion files under /etc/bash_completion.d/?Gorges
most are under the /usr/local/etc/bash_completion.d dirGlottis
No, bash does not load these automatically. But one of the standard startup scripts might...Warrantee
should I be sourceing the completion scripts or evaling them?Glottis
A
5

This varies depending on your platform and/or versions of bash and bash-completion. For example:

Ubuntu

On Ubuntu 20.04 the file /etc/bash_completion does this:

. /usr/share/bash-completion/bash_completion

And in that file I find

for dir in ${XDG_DATA_DIRS:-/usr/local/share:/usr/share}; do
    dirs+=( $dir/bash-completion/completions )
done

Which indicates that /usr/local/share/bash-completion/completions is scanned for completion scripts. Empirical experiments supports this.

MacOS/Brew

I could not find anything about bash completion in /etc or /usr/share on my MacOS 11.2.3. Which indicates that bare Darwin does not have bash completion, which makes sense since Apple have left Bash at 3.2 because of licensing. Might have for zsh, though, I didn't look.

/usr/local/etc/bash-completion.d, which you reference, is a part of the Homebrew installation under /usr/local. In there I found some completion scripts, but not the activation script. You should not have to activate those explicitly yourself.

I did find /usr/local/share/bash-completion, again from Homebrew, and it has the script bash_completion. In it are the same lines as Ubuntu, which also makes sense since Homebrew is kinda complete "GNU" but under /usr/local. But it also reference the directory /usr/local/etc/bash-completion.d. Sourcing /usr/local/share/bash-completion/bash_completion added the completion from that directory also.

But /usr/local/share/bash-completion/bash_completion is not executed by default, so you have to add that to your ~/bash_profile or ~/profile as described here. It also describes how to handle zsh and fish.

Cygwin

Cygwin is another Posix-compliant environment which has bash completion. (I haven't checked if bash completion is part of the Posix standard, though) After installing the bash-completion package there is /usr/share/bash-completion/bash_completion as Ubuntu and Homebrew has. Here there is no /etc/bash_completion and as the ~/.bashrc I had (generated long ago) did only look for this completions wasn't activated.

Summary

Many GNU-like environments support bash_completion but you might have to

  • install a package
  • ensure that it is sourced when you log in, which is not always the case by default

If it is not activated by default in your environment, you can activate it by sourcing the "root" script (in /etc, /usr/share/bash-completion or where it might be located) from your .bashrc, bash_profile or similar.

I'm guessing YMMV for all other possible platforms (other Linux distros, MSYS2 etc.) but the above might help you to figure out how to enable completion. Which is really helpful when available.

Annunciator answered 21/3, 2021 at 11:42 Comment(2)
well, it's not really platform, it's just bash-completion version. The first versions didn't load anything, then /etc/bash-completion.d was loaded, and as development goes additional directories were added.Apriorism
@Apriorism yes, that's one part (clarified that) but not "just". "platform" (whatever we might mean by that) also differs depending on how "automatic" activation is. In Homebrew and Cygwin you have to add somethings to your startup scripts to activate completion.Annunciator

© 2022 - 2024 — McMap. All rights reserved.