How to add bash auto completion in Ubuntu Linux

I set up a new Ubuntu Linux LXD container, but my auto-completion of bash shell commands are not working. How do I enable autocomplete feature in Ubuntu Linux 18.04/20.04 LTS? How do I write simple bash completion code in Ubuntu Linux?

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 Ubuntu Linux. This page shows how to install and enable Bash auto completion in Ubuntu Linux.

ADVERTISEMENTS

How to add bash auto completion in Ubuntu Linux

The procedure is as follows to add bash completion in Ubuntu:

  1. Open the terminal application
  2. Refresh package database on Ubuntu by running: sudo apt update
  3. Install bash-completion package on Ubuntu by running: sudo apt install bash-completion
  4. Log out and log in to verify that bash auto completion in Ubuntu Linux working properly

Let us see all steps in details.

How to add bash completion In Ubuntu

Bash completion is a useful tool for the automatically completing of file names, commands and more. Type the following apt command/apt-get command to install auto completion in Ubuntu Linux:
$ sudo apt update
$ sudo apt install bash-completion

Command to add bash auto completion in Ubuntu Linux

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

Ubuntu bash_completion.sh to turn on bash smart completion

Bash shell code that turn on bash smart completion feature

You can add /etc/profile.d/bash_completion.sh to your ~/.bashrc file as follows:

## source it from ~/.bashrc or ~/.bash_profile ##
echo "source /etc/profile.d/bash_completion.sh" >> ~/.bashrc
## Another example Check and load it from ~/.bashrc or ~/.bash_profile ##
grep -wq '^source /etc/profile.d/bash_completion.sh' ~/.bashrc || echo 'source /etc/profile.d/bash_completion.sh'>>~/.bashrc

How do I enable bash completion on Ubuntu and use it from the current session

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 apt i[TAB] $ cat /etc/re[TAB]

Writing your first simple bash completion code

Say you always want to check an IP address of three domain names using the host command. So type the following at the shell prompt:
complete -W 'google.com cyberciti.biz nixcraft.com' host
Now type host and press the [TAB] key to use any one of the domain name for host command:
host [TAB]

Bash host command auto completion

host command with bash completion on my Ubuntu 18.04

Conclusion

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

This entry is 4 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 VPNAlpine CentOS 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.