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:
- Open the terminal application
- Refresh package database on Ubuntu by running:
$ sudo apt update - Get info about the bash-completion package:
$ apt info bash-completion - Install bash-completion package on Ubuntu by running:
$ sudo apt install bash-completion - 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
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
Bash shell code that turn on bash smart completion feature
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]
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
π₯Ί Was this helpful? Please add a comment to show your appreciation or feedback.
thank you.
Same. ++thank you <3