Install
# complete is a Bash builtin. It is available whenever you are using bash. # Optional (recommended): bash-completion package provides many ready-made completions. # RHEL / Alma / Rocky sudo dnf install bash-completion # Debian / Ubuntu sudo apt install bash-completion # openSUSE sudo zypper install bash-completion # Arch sudo pacman -S bash-completion
What it does
complete registers programmable completion rules for commands in Bash. It lets you define what suggestions appear when you press Tab. You can also use it to inspect existing completion definitions.
How it works (mechanical)
- Hooks into Bash’s programmable completion system.
- Associates a command name with a completion “spec”.
- Completion specs can be simple lists, file/dir matches, or functions.
- Often used via helper functions like
_filedirfrombash-completion. - Rules live in the current shell unless loaded from startup scripts.
Quick Start
# Show completion rules currently registered complete -p
10 Practical Examples
# 1) List all completion definitions complete -p
# 2) Show completion rule for a specific command (if present) complete -p git
# 3) Remove completion for a command complete -r mycmd
# 4) Simple completion: provide fixed words # Tab after "hello " suggests: world there friend complete -W "world there friend" hello
# 5) Complete only directories for a command complete -d cdto
# 6) Complete only filenames for a command complete -f openfile
# 7) Complete based on a function (advanced)
_mytool_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
COMPREPLY=( $(compgen -W "start stop status restart" -- "$cur") )
}
complete -F _mytool_complete mytool# 8) Enable bash-completion (common install check) # On many distros this is auto, but to force it in a session: source /usr/share/bash-completion/bash_completion
# 9) Persist a completion rule in your shell config # Add to ~/.bashrc (example): # complete -W "dev prod stage" deployenv
# 10) Debug what Bash thinks the current completion context is # Run inside a completion function: # declare -p COMP_WORDS COMP_CWORD
Notes & Gotchas
- Works in Bash; other shells have different completion systems (zsh, fish).
- Completion rules are per-session unless loaded from
~/.bashrcor system scripts. - The
bash-completionpackage provides most “magic” completions people expect (git, systemctl, kubectl, etc.). - If completion seems broken, check that your shell is interactive and
bash_completionis sourced. - For scripts, avoid relying on completion behavior — it’s an interactive feature.
Historical Context
Early shells offered basic filename completion. Bash introduced programmable completion to allow
context-aware suggestions (options, subcommands, resources), and distributions later standardized
common completions via the bash-completion project.
Modern Equivalent
Bash programmable completion remains widely used. Other shells (like zsh) provide more advanced
frameworks, but Bash + bash-completion is still the standard on many servers.
Related Commands
- compgen — generate possible completion matches.
- compopt — modify completion options within a completion function.
- bind — manage Readline key bindings (Tab key behavior depends on Readline settings).
- bash — the shell providing programmable completion.
- source — load completion definitions into the current shell.