How to install and enable Bash auto completion in Fedora Linux

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.

ADVERTISEMENTS

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.

This entry is 5 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
FirewallCentOS 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
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.