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
Now all you have to do is type the following dnf command to install it:

$ sudo dnf install bash-completion

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 called automatically from your login session or when you start a fresh shell session. To load the current session use the source command:

$ 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

Ping three domain names using the ping command. So type the following at the shell prompt:

complete -W 'google.com ucartz.com rootadminz.com' ping


Now type ping and press the [TAB] key to use any one of the domain names for ping command:

ping [TAB]

Let us write a simple function:

# simple script for ping command 
# Include all host names from /etc/hosts file too
_ping () {  COMPREPLY=(ucartz.com google.com rootadminz.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 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]


That's it !!

هل كانت المقالة مفيدة ؟ 0 أعضاء وجدوا هذه المقالة مفيدة (0 التصويتات)