How to enable command completion in Bash

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.

Steps to enable command completion in Bash:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. Add the bash-completion loader to ~/.bashrc only when the previous check printed no handler.
    ~/.bashrc
    . /etc/bash_completion
  7. Reload ~/.bashrc in the current terminal.
    $ source ~/.bashrc
  8. 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.