How to add bash auto completion in CentOS 8 Linux

ADVERTISEMENTS



How to add bash auto completion in CentOS 8

I set up a new cloud server, but my auto-completion of bash shell commands are not working. How do I enable autocomplete feature in CentOS 8 (CentOS Enterprise Linux 8)? How do I write simple bash completion code in CentOS 8?Introduction: Bash is a free and open source shell. It is an sh-compatible command language interpreter that executes commands read from the standard input or from a file. Bash can run most sh scripts without modification. bash-completion is a collection of shell functions that take advantage of the programmable completion feature of bash on CentOS 8. This page shows how to install and enable Bash auto completion in CentOS 8.

The procedure is as follows to add bash completion in CentOS Enterprise Linux 8:

Open the terminal window/bash shell prompt Search for bash-completion on CentOS 8 by running: sudo yum search bash-completion Install completion for bash on CentOS 8 by running: sudo yum install bash-completion Log out and log in to verify that bash auto completion in CentOS 8 working as desired

Let us see all steps and examples in details.

Find info about the bash-completion package

One can search it as follows:

yum search bash-completion

Sample outputs:

Last metadata expiration check: 0 :06: 40 ago on Wed Dec 11 17 :03: 53 2019 . ==================== Name Exactly Matched: bash-completion = =================== bash-completion.noarch : Programmable completion for Bash = ======================= Name Matched: bash-completion ======================== nbdkit-bash-completion.noarch : Bash tab-completion for nbdkit libvirt-bash-completion.x86_64 : Bash completion script libguestfs-bash-completion.noarch : Bash tab-completion scripts for libguestfs tools coccinelle-bash-completion.noarch : Bash tab-completion for coccinelle python-django-bash-completion.noarch : bash completion files for Django Last metadata expiration check: 0:06:40 ago on Wed Dec 11 17:03:53 2019. ==================== Name Exactly Matched: bash-completion ==================== bash-completion.noarch : Programmable completion for Bash ======================== Name Matched: bash-completion ======================== nbdkit-bash-completion.noarch : Bash tab-completion for nbdkit libvirt-bash-completion.x86_64 : Bash completion script libguestfs-bash-completion.noarch : Bash tab-completion scripts for libguestfs tools coccinelle-bash-completion.noarch : Bash tab-completion for coccinelle python-django-bash-completion.noarch : bash completion files for Django

Another option is just list the package on CentOX 8 box:

yum list bash-completion

Sample outputs:

Last metadata expiration check: 0:06:06 ago on Wed Dec 11 17:03:53 2019. Available Packages bash-completion.noarch 1:2.7-5.el8 BaseOS

How to add bash completion CentOS 8

Bash completion is a useful tool for the automatically completing of file names, commands, directory names, hostnames and more. Type the following yum command to install auto completion in CentOS 8:

sudo yum install bash-completion

OR

sudo dnf install bash-completion



How to test programmable completion for Bash

The above command placed a shell script called /etc/profile.d/bash_completion.sh. You can view it with help of more command or cat command:

more /etc/profile.d/bash_completion.sh

OR

cat /etc/profile.d/bash_completion.sh

Sample outputs:

# Check for interactive bash and that we haven't already been sourced in CentOS server if [ -n " ${BASH_VERSION-} " -a -n " ${PS1-} " -a -z " ${BASH_COMPLETION_VERSINFO-} " ] ; then # Check for recent enough version of bash. if [ ${BASH_VERSINFO[0]} -gt 4 ] || \ [ ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 ] ; then [ -r " ${XDG_CONFIG_HOME:-$HOME/.config} /bash_completion" ] && \ . " ${XDG_CONFIG_HOME:-$HOME/.config} /bash_completion" if shopt -q progcomp && [ -r / usr / share / bash-completion / bash_completion ] ; then # Source completion code. . / usr / share / bash-completion / bash_completion fi fi fi fi # Check for interactive bash and that we haven't already been sourced in CentOS server if [ -n "${BASH_VERSION-}" -a -n "${PS1-}" -a -z "${BASH_COMPLETION_VERSINFO-}" ]; then # Check for recent enough version of bash. if [ ${BASH_VERSINFO[0]} -gt 4 ] || \ [ ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 ]; then [ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" ] && \ . "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" if shopt -q progcomp && [ -r /usr/share/bash-completion/bash_completion ]; then # Source completion code. . /usr/share/bash-completion/bash_completion fi fi fi fi

How do I enable bash completion on a CentOS 8 and use it from the current session

The above script will get call automatically from your login session or when you start a fresh shell session. For the current session use the source command to load it:

source /etc/profile.d/bash_completion.sh

Press the [TAB] key while typing a command to auto-complete syntax or options:

sudo dnf i[TAB] cat /etc/ho[TAB]

Writing your first simple bash completion code on CentOS 8

Say you always want to ping an IP address/host names of three servers using the ping command. So type the following at the shell prompt:

complete -W '192.168.2.254 cyberciti.biz google.com' ping

Now type ping and press the [TAB] key to use any one of the domain names/IP address for ping command:

ping [TAB]



Conclusion

The easiest way to install bash completion software is to use a package manager such as yum command. You installed the bash-completion package and learned how to create simple function and wrappers on CentOS 8. For more info see this page.

