RHEL / CentOS Linux install and activate Bash completion

How do I use bash auto complete features with various command on CentOS / RHEL / Fedora / Scientific Linux based systems?

Tutorial details
DifficultyEasy (rss)
Root privilegesYes
RequirementsEPEL
TimeN/A
Autocomplete is nothing but a collection of various hacks that specify how arguments are to be completed by Readline using complete built-in. By default, this feature is turned on many Linux distributions such as Debian Linux, Ubuntu Linux and more. However, this feature is not installed and turned on on RHEL based Linux distributions including CentOS Linux. This tutorials shows you how to install Bash Autocomplete feature on a CentOS/RHEL/Oracle/Scientific or Fedora Linux.

ADVERTISEMENTS

Packages to install and enable Bash completion on CentOS/RHEL

You need to install two packages:

  1. bash-completion: Programmable completion for Bash
  2. bash-completion-extras : Additional programmable completions for Bash (only for CentOS/RHEL/Oracle/SL v7.x)

RHEL / CentOS Linux install and activate Bash completion

First, turn on the EPEL repo on a CentOS/RHEL version 6 (if you are using CentOS/RHEL 7.x, see this page to turn on EPEL repo) and type the following yum command to install bash-completion package:
# yum install bash-completion
OR
# yum install bash-completion bash-completion-extras
Sample outputs:

Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: ftp.iitm.ac.in
 * epel: ftp.cuhk.edu.hk
 * extras: ftp.iitm.ac.in
 * updates: ftp.iitm.ac.in
Resolving Dependencies
--> Running transaction check
---> Package bash-completion.noarch 1:2.1-6.el7 will be installed
---> Package bash-completion-extras.noarch 1:2.1-11.el7 will be installed
--> Finished Dependency Resolution
 
Dependencies Resolved
 
=======================================================================================================
 Package                            Arch               Version                  Repository        Size
=======================================================================================================
Installing:
 bash-completion                    noarch             1:2.1-6.el7              base              85 k
 bash-completion-extras             noarch             1:2.1-11.el7             epel             123 k
 
Transaction Summary
=======================================================================================================
Install  2 Packages
 
Total download size: 208 k
Installed size: 648 k
Is this ok [y/d/N]: y
Downloading packages:
(1/2): bash-completion-2.1-6.el7.noarch.rpm                                     |  85 kB  00:00:03     
(2/2): bash-completion-extras-2.1-11.el7.noarch.rpm                             | 123 kB  00:00:07     
-------------------------------------------------------------------------------------------------------
Total                                                                   26 kB/s | 208 kB  00:00:07     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 1:bash-completion-2.1-6.el7.noarch                                                  1/2 
  Installing : 1:bash-completion-extras-2.1-11.el7.noarch                                          2/2 
  Verifying  : 1:bash-completion-extras-2.1-11.el7.noarch                                          1/2 
  Verifying  : 1:bash-completion-2.1-6.el7.noarch                                                  2/2 
 
Installed:
  bash-completion.noarch 1:2.1-6.el7             bash-completion-extras.noarch 1:2.1-11.el7            
 
Complete!

Fedora Linux user type the following dnf command:
$ sudo dnf install bash-completion

Related: How to install and enable Bash auto completion in Fedora Linux

How do I use bash Autocomplete feature?

Then exits a login shell / ssh session and log back in using the following command:
# logout
Open a terminal or log in using ssh:
$ ssh nixcraft@server1.cyberciti.biz
Alternatively, use the following command to source completion code for bash:
$ . /etc/bash_completion
OR
$ source /etc/bash_completion
For newer version try:
$ source /etc/profile.d/bash_completion.sh

Usage examples

You need to simply press [TAB] key. The syntax is:

command [TAB]
command arg[TAB][TAB]
man da[TAB]

rpm usage

Type the following command (type rpm -qi ph followed by [TAB]):

$ rpm -qi ph[TAB][TAB]
php         php-cli     php-common  php-devel   php-imap

yum usage

Type the following command:

$ yum [TAB][TAB]
--assumeyes        --enableplugin     list               search
--cacheonly        --enablerepo       makecache          --setopt
check              --errorlevel       --nogpgcheck       shell
check-update       --exclude          --noplugins        --showduplicates
clean              groupinfo          --obsoletes        --skip-broken
--color            groupinstall       provides           --tolerant
--config           grouplist          --quiet            update
--debuglevel       groupremove        --randomwait       upgrade
deplist            help               reinstall          --verbose
--disableexcludes  --help             --releasever       version
--disableplugin    history            remove             --version
--disablerepo      info               repolist           
distro-sync        install            resolvedep         
downgrade          --installroot      --rpmverbosity 

To search a package called lsof:
$ yum se[TAB] lsof
Or search and install on fly:

# yum in[TAB] ls[TAB]
lshell.noarch            lslk-debuginfo.x86_64    lsscsi-debuginfo.x86_64
lshw-gui.x86_64          lslk.x86_64              lsscsi.x86_64
lshw.x86_64              lsof-debuginfo.x86_64    lsyncd.x86_64

cryptsetup usage

Linux Unified Key Setup-on-disk-format (LUKS) allows you to encrypt whole disks and you can use cryptsetup command to mount or remount encrypted partition:
cryptsetup [TAB] luksOpen /dev/md[TAB]
Sample outputs:

RHEL / CentOS Linux install and activate Bash completion

Fig.01: cryptsetup on RHEL based system

A list of all commands supported by Autocomplete feature

Type the following command:
$ cd /etc/bash_completion.d
$ ls

Sample outputs:

autoconf       gpg2               ntpdate           shadow
automake       gzip               open-iscsi        smartctl
bash-builtins  iconv              openssl           sqlite3
bind-utils     iftop              perl              ssh
brctl          ifupdown           pkg-config        strace
bzip2          info               pm-utils          subscription-manager
chkconfig      ipmitool           postfix           tar
configure      iproute2           procps            tcpdump
coreutils      iptables           python            util-linux
cpio           lsof               quota-tools       wireless-tools
crontab        lvm                redefine_filedir  xmllint
cryptsetup     lzma               rfkill            xmlwf
dd             make               rpm               xz
dhclient       man                rsync             yum.bash
e2fsprogs      mdadm              scl.bash          yum-utils.bash
findutils      module-init-tools  service
getent         net-tools          sh

Conclusion

We explained how to install and use the bash-completion package under RHEL and CentOS Linux. For more info see this page.

This entry is 3 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

ADVERTISEMENTS
8 comments… add one
  • Prasad Mar 15, 2013 @ 5:47

    Thanks for sharing

  • Dariusz Mar 15, 2013 @ 9:23

    wow, that is what I have seeking looking for.
    It is what Cisco and other companies have been doing for a while for them CLI.

  • Alan Apr 12, 2013 @ 5:50

    Awesome article.

  • Alan Apr 12, 2013 @ 6:07

    But i dont find this package to be installed for Cent OS 6.4.

  • hoppie May 16, 2013 @ 15:56

    OK how on earth can anyone run a ‘YUM’ command without yum being installed? or even downloaded?

    Tar very much, Bibibibiii

  • Jayakrishnan Nair Jan 14, 2014 @ 3:48

    I have getting error on running yum install bash-completion

    Error: rpmdevtools conflicts with bash-completion-20060301-1.el6.rf.noarch
     You could try using --skip-broken to work around the problem
     You could try running: rpm -Va --nofiles --nodigest
    

    Should i continue with option recommended by yum. Any other stable build available.

  • Julian Nov 21, 2014 @ 9:51

    This works as well:
    yum install bash-completion
    vim ~/.bashrc

    Add this line
    source /etc/bash_completion.d/git
    Save and then
    source ~/.bashrc

  • Wawrzyniec Pruski Mar 22, 2016 @ 15:03

    Jayakrishnan Nair,

    Run this and then it should work:
    sudo yum remove rpmdevtools

Leave a Reply

Your email address will not be published.

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