For non-XDG environments and bash-completion <= 2.8
Here is how a local user can have a working ~/.bash_completion.d/
directory.
- edit file:
nano ~/.bash_completion
, add the following:
for bcfile in ~/.bash_completion.d/* ; do
. $bcfile
done
- make directory:
mkdir ~/.bash_completion.d
- edit file:
~/.bash_completion.d/myscript
, add the following:
_myscript_tab_complete () {
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
words="-f -h --etc"
COMPREPLY=( $(compgen -W "${words}" -- ${cur}) )
return 0
}
complete -F _myscript_tab_complete myscript
- source .bash_completion:
. ~/.bash_completion
anticipating empty ~/.bash_completion.d/ directory
If you anticipate having an empty ~/.bash_completion.d/
directory, and want to avoid seeing the error message bash: /home/<username>/.bash_completion.d/*: No such file or directory
, add a file type test with [ -f "$bcfile" ]
.
for bcfile in ~/.bash_completion.d/* ; do
[ -f "$bcfile" ] && . $bcfile
done
For XDG environments & bash-completion <= version 2.8
(To see your version: rpm -qa | grep bash-completion
)
Note that bash-completion 2.8 README documentation still recommends ~/.bash_completion
, See https://github.com/scop/bash-completion/tree/2.8
You know your linux environment is XDG if xdg-usr-dir
command exists and returns your home directory, and your environment variables include XDG_*
; execute: env | grep XDG
.
For XDG environment & bash-completion >= version 2.9
According to the bash-completion README starting in version 2.9 https://github.com/scop/bash-completion/tree/2.9 (April 27, 2019) there is now recognized a working local user bash_completion directory. Additionally an alternate bash_completion config file has already been supported for several previous releases.
~/.bash_completion.d
-> ${XDG_DATA_HOME:-$HOME/.local/share}/bash-completion
~/.bash_completion
-> ${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion
To implement with this configuration, complete the previous steps 3,4 above using $XDG_DATA_HOME/bash-completion
- you should not modify $XDG_CONFIG_HOME/bash_completion
as described in step 1, but you may need to create the $XDG_DATA_HOME/bash-completion
directory.
Note: As of CentOS 8.2 and Fedora 32, bash-completion <= 2.8 is installed.