How to add bash auto completion in RHEL 8 (Red Hat Linux)

ADVERTISEMENTS



How to add bash auto completion in RHEL 8

I set up a new RHEL 8 VM from cloud images , but my auto-completion of bash shell commands are not working. How do I enable autocomplete feature in RHEL 8 (Red Hat Enterprise Linux 8)? How do I write simple bash completion code in RHEL 8?Introduction: Bash 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 RHEL 8. This page shows how to install and enable Bash auto completion in RHEL 8.

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

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

Let us see all steps in details.

How to add bash completion RHEL 8

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

$ sudo yum install bash-completion

OR

$ sudo dnf install bash-completion



How to test programmable completion for Bash

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

$ cat /etc/profile.d/bash_completion.sh

Sample outputs:

# Check for interactive bash and that we haven't already been sourced. 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 # Check for interactive bash and that we haven't already been sourced. 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

How do I enable bash completion on RHEL 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 yum i[TAB] $ cat /etc/re[TAB]

Writing your first simple bash completion code on RHEL 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 RHEL 8. For more info see this page.

