Command completion in Bash lets the shell offer command-aware matches when Tab is pressed, including subcommands, options, and arguments provided by installed completion scripts. Basic filename completion can work without extra packages, but command-specific completion needs Bash's programmable completion layer and a completion loader.
On Ubuntu and Debian systems, the bash-completion package installs a shared completion loader and the /etc/bash_completion compatibility entry point. Current default Ubuntu startup files already source one of those paths for interactive shells, but older or custom ~/.bashrc files may need the loader added manually.
The package is enabled when an interactive Bash shell can find _comp_complete_load, the on-demand loader function. If that check already prints the function name after reloading ~/.bashrc, the completion loader is active and no startup-file edit is needed.
Related: How to configure Bash login scripts
Related: How to enable bash completion in macOS
Related: How to generate shell completions for Codex
Steps to enable command completion in Bash:
- Confirm that programmable completion is enabled in the current Bash shell.
$ shopt progcomp progcomp on
If this returns off, run shopt -s progcomp in the interactive shell or remove the startup-file line that disables progcomp.
- Install the bash-completion package on Ubuntu or Debian.
$ sudo apt install bash-completion
The package manager prints the normal install summary and finishes by setting up the bash-completion package.
- Confirm that the package installed the Debian and Ubuntu compatibility loader.
$ ls /etc/bash_completion /etc/bash_completion
Use the existing distro snippet if ~/.bashrc already has one. The short manual snippet below is for custom startup files that do not load bash-completion yet.
- Back up ~/.bashrc before changing interactive shell startup behavior.
$ cp ~/.bashrc ~/.bashrc~
If ~/.bashrc does not exist yet, create it with touch ~/.bashrc before checking or adding the completion loader.
- Reload ~/.bashrc and check whether the loader function is already present.
$ source ~/.bashrc $ f=_comp_complete_load $ declare -F "$f" _comp_complete_load
If this command prints _comp_complete_load, skip the ~/.bashrc edit and open a new terminal before using command-specific completion.
- Add the bash-completion loader to ~/.bashrc only when the previous check printed no handler.
- ~/.bashrc
. /etc/bash_completion
- Reload ~/.bashrc in the current terminal.
$ source ~/.bashrc
- Confirm that the interactive shell can find the completion loader function.
$ f=_comp_complete_load $ declare -F "$f" _comp_complete_load
The _comp_complete_load function is the bash-completion on-demand loader. It loads command-specific completion files when completion is requested for a command.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.