How to install and enable Bash auto completion in Fedora Linux

last updated in Categories , ,

I am a new Fedora Linux user. How do I install and enable Bash auto completion in Fedora Linux server?

Introduction: The GNU Bourne Again shell (Bash) is a shell for Fedora Linux. It is also act as command language interpreter that is compatible with the Bourne shell (sh). Bash combines useful features from the Korn shell (ksh) and the C shell csh. 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 Fedora Linux. This page shows how to install and enable Bash auto completion in Fedora Linux.

How do I install and enable Bash auto completion in Fedora Linux?

You need to use the dnf command:
$ dnf search bash-completion
To get info just run the dnf command with info option:
$ dnf info bash-completion
dnf search programmable completion for Bash on Fedora
Now all you have to do is type the following dnf command to install it:
$ sudo dnf install bash-completion
Install and enable Bash auto completion in Fedora Linux using dnf

Related: RHEL / CentOS: Install and Activate Bash Autocomplete Feature

How to test programmable completion for Bash

Installer placed a script called /etc/profile.d/bash_completion.sh. You can view it with the 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

The 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]

How to write simple bash completion

Say you always want to ping three domain names using the ping command. So type the following at the shell prompt:
complete -W 'google.com cyberciti.biz nixcraft.com' ping
Now type ping and press the [TAB] key to use any one of the domain name for ping command:
ping [TAB]
your first bash auto completion code
Let us write a simple function:

# simple script for ping command 
# Include all host names from /etc/hosts file too
_ping () {  COMPREPLY=(cyberciti.biz google.com nixcraft.com yahoo.com $(awk '{print $2}' /etc/hosts | uniq) ); }
complete -F _ping ping

Run it as follows:
ping [TAB]

How to reuse existing completions

I am going to reuse /usr/share/bash-completion/completions/lxd.lxc (i.e. used by the LXD server in Fedora) in my custom bash shell script wrapper:

_mylxc () {
    source /usr/share/bash-completion/completions/lxd.lxc && _complete_from_snap
}
# note cbzlxc is my custom made wrapper and is in $HOME/bin/
complete -F _mylxc cbzlxc

Now run it as follows:
cbzlxc [TAB]

Conclusion

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

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 0 comments/add one below):

Join the discussion at www.nixcraft.com