Bash: Where to install completion?

From FVue
Jump to: navigation, search

Problem

I have written bash dots functions with autocompletion. What is the best place to install them?

Solution

Eh, multiple solutions? Maybe install function and completion separate, for all users at once. Drawback is that functions have to be exported to have them available in a non-login interactive shell?

/etc/profile.d/           dots-functions.sh
/etc/bash_completion.d/   dots-completion.sh

Or creating a directory /etc/bash.bashrc.d which is executed from /etc/bash.bashrc. This has the benefit of easy upgrading individual scripts. And it's only sourced when necessary — within an interactive shell. And it's available for all users.

for s in /etc/bash.bashrc.d/*.sh ; do
    test -r $s && . $s
done
unset s

Or for a single user put files in a user directory ~/.bashrc.d invoked from ~/.bashrc, which makes it easy to upgrade individual scripts:

for s in ~/.bashrc.d/*.sh ; do
    test -r $s && . $s
done
unset s

See also

What happens when you invoke bash?

Comments

blog comments powered by Disqus