How to add bash auto completion in CentOS 8 Linux

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.


How to add bash auto completion in CentOS 8

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

  1. Open the terminal window/bash shell prompt
  2. Search for bash-completion on CentOS 8 by running: sudo yum search bash-completion
  3. Install completion for bash on CentOS 8 by running: sudo yum install bash-completion
  4. 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

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
sudo dnf install bash-completion
Bash auto completion in CentOS 8 [CentOS Enterprise Linux]

How to test programmable completion for Bash

The above command placed a shell script called /etc/profile.d/ You can view it with help of more command or cat command:
more /etc/profile.d/
cat /etc/profile.d/
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

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/
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 '' ping
Now type ping and press the [TAB] key to use any one of the domain names/IP address for ping command:
ping [TAB]
CentOS 8 bash auto completion command


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.

This entry is 1 of 5 in the Linux / Unix Bash Auto Completion Tutorial series. Keep reading the rest of the series:
  1. Add bash auto completion in CentOS 8 Linux
  2. How to add bash auto completion in RHEL 8 (Red Hat Linux)
  3. RHEL / CentOS Linux install and activate Bash completion
  4. Ubuntu Linux add bash auto completion
  5. Fedora Linux install and enable Bash auto completion
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

0 comments… add one

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.