How to add bash auto completion in Ubuntu Linux

See all Ubuntu Linux related FAQ
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.
Tutorial details
Difficulty level Easy
Root privileges Yes
Requirements Linux terminal
Category Package Manager
OS compatibility Debian β€’ Mint β€’ Pop!_OS β€’ Ubuntu
Est. reading time 3 minutes

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. Get info about the bash-completion package:$ apt info bash-completion
  4. Install bash-completion package on Ubuntu by running:$ sudo apt install bash-completion
  5. Log out and log in to verify that bash auto completion in Ubuntu Linux working properly.

Let us see all steps in details for Ubuntu Linux system.

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 upgrade
$ 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:

1
2
3
4
5
## 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

The last example (see line #5) is using the grep command/egrep command to search for a string named β€˜bash_completion.sh’, and if the string is not found, the echo command will append it to the ~/.bashrc file. The || act as logical OR (||). It is the boolean operator. It can execute commands or shell functions based on the exit status of the grep command.

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. Also, read the bash man page using the man command or help command:
$ man bash
$ help complete

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

πŸ₯Ί Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Vivek Gite is an expert IT Consultant with over 25 years of experience, specializing in Linux and open source solutions. He writes about Linux, macOS, Unix, IT, programming, infosec, and open source. Follow his work via RSS feed or email newsletter.

Category List of Unix and Linux commands
AnsibleCheck version β€’ Fedora β€’ FreeBSD β€’ Linux β€’ Ubuntu 18.04 β€’ Ubuntu β€’ macOS
Archivingz commands
Backup ManagementDebian/Ubuntu β€’ FreeBSD β€’ RHEL
Database ServerBackup MySQL server β€’ MariaDB Galera cluster β€’ MariaDB TLS/SSL β€’ MariaDB replication β€’ MySQL Server β€’ MySQL remote access
Download managerswget
Driver ManagementLinux Nvidia driver β€’ lsmod
Documentationhelp β€’ mandb β€’ man β€’ pinfo
Disk Managementdf β€’ duf β€’ ncdu β€’ pydf
File Managementcat β€’ cp β€’ less β€’ mkdir β€’ more β€’ tree
FirewallAlpine Awall β€’ CentOS 8 β€’ OpenSUSE β€’ RHEL 8 β€’ Ubuntu 16.04 β€’ Ubuntu 18.04 β€’ Ubuntu 20.04 β€’ Ubuntu 24.04
KVM VirtualizationCentOS/RHEL 7 β€’ CentOS/RHEL 8 β€’ Debian 9/10/11 β€’ Ubuntu 20.04
Linux Desktop appsChrome β€’ Chromium β€’ GIMP β€’ Skype β€’ Spotify β€’ VLC 3
LXDBackups β€’ CentOS/RHEL β€’ Debian 11 β€’ Fedora β€’ Mount dir β€’ Ubuntu 20.04 β€’ Ubuntu 22.04
Modern utilitiesbat β€’ exa
Network ManagementMonitoring tools β€’ Network services β€’ RHEL static IP β€’ Restart network interface β€’ nmcli
Network UtilitiesNetHogs β€’ dig β€’ host β€’ ip β€’ nmap β€’ ping
OpenVPNCentOS 7 β€’ CentOS 8 β€’ Debian 10 β€’ Debian 11 β€’ Debian 8/9 β€’ Ubuntu 18.04 β€’ Ubuntu 20.04
Power Managementupower
Package Managerapk β€’ apt-get β€’ apt β€’ yum
Processes Managementbg β€’ chroot β€’ cron β€’ disown β€’ fg β€’ glances β€’ gtop β€’ iotop β€’ jobs β€’ killall β€’ kill β€’ pidof β€’ pstree β€’ pwdx β€’ time β€’ vtop
Searchingag β€’ egrep β€’ grep β€’ whereis β€’ which
Shell builtinscompgen β€’ echo β€’ printf
System Managementreboot β€’ shutdown
Terminal/sshsshpass β€’ tty
Text processingcut β€’ rev
Text Editor6 Text editors β€’ Save and exit vim
User Environmentexit β€’ who
User Informationgroups β€’ id β€’ lastcomm β€’ last β€’ lid/libuser-lid β€’ logname β€’ members β€’ users β€’ whoami β€’ w
User Management/etc/group β€’ /etc/passwd β€’ /etc/shadow β€’ chsh
Web ServerApache β€’ Let's Encrypt certificate β€’ Lighttpd β€’ Nginx Security β€’ Nginx
WireGuard VPNAlpine β€’ Amazon Linux β€’ CentOS 8 β€’ Debian 10 β€’ Firewall β€’ Ubuntu 20.04 β€’ qrencode
2 comments… add one

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.