compgen
Generate command completion suggestions
TLDR
List all shell built-ins, aliases, functions, and executables that you could run
List all commands that you could run that start with a specified string and save results to $COMPREPLY
Match against a wordlist
List all aliases
List all functions that you could run
Show shell reserved keywords
See all available commands/aliases starting with ls
List all users on the system
SYNOPSIS
compgen [-A action] [-bcdfghjksuv] [-G globpat] [-o option] [-W wordlist] [-F functionname] [-C commandname] [-X filterpat] [-P prefix] [-S suffix] [word]
PARAMETERS
-A action
Generate completions based on action: alias, arrayvar, binding, builtin, command, directory, disabled, enabled, export, file, function, group, help, hostname, job, keyword, running, service, setopt, signal, stopped, user
-b
Shortcut for -A builtin
-c
Shortcut for -A command
-d
Shortcut for -A directory
-f
Shortcut for -A file
-g
Shortcut for -A group
-j
Shortcut for -A job
-k
Shortcut for -A keyword
-s
Shortcut for -A signal
-u
Shortcut for -A user
-v
Shortcut for -A export
-G globpat
Filenames matching shell glob pattern globpat
-o option
Completion behavior: bashdefault, default, dirnames, filenames, filenamedirs, nospace
-W wordlist
Completions from colon-separated wordlist
-F functionname
Completions from output of function functionname
-C commandname
Completions from output of command commandname
-X filterpat
Exclude matches with glob pattern filterpat; negate with '!' prefix
-P prefix
Prepend prefix to each completion
-S suffix
Append suffix to each completion
DESCRIPTION
compgen is a Bash shell built-in command that generates lists of possible completions for command-line arguments, mimicking the behavior of the Tab key completion. It is primarily used within Bash's programmable completion system but can be invoked directly from the command line or scripts to output words matching specified criteria.
This tool is invaluable for shell scripting, custom completion functions, and debugging completion setups. By specifying options like -A for actions (e.g., aliases, functions, commands), -G for glob patterns, or -W for word lists, compgen filters and generates relevant completions against an optional word argument. For instance, it can list all available commands starting with 'ls' or all exported variables.
Unlike external tools, compgen leverages Bash's internal data structures for efficiency, making it fast for tasks like listing jobs, hostnames, or services. Output can be customized with prefixes (-P), suffixes (-S), and filters (-X). It's often paired with complete to define dynamic completions, enhancing interactive shell experience. While powerful, it's Bash-specific and unavailable in other shells like Zsh or Fish without emulation.
CAVEATS
Bash built-in only; unavailable in non-Bash shells. Output depends on current shell state (e.g., jobs, variables). Multiple options may combine unpredictably. Not suitable for non-interactive scripts expecting stable output.
EXAMPLES
compgen -c ls
List commands starting with 'ls'. compgen -A function
List all shell functions. compgen -G '*.sh' -- '*.sh'
Match shell glob files. compgen -W 'start stop restart' -- sta
Complete from wordlist matching 'sta'.
HISTORY
Introduced in Bash 1.14.0 (1994) as part of early programmable completion features. Evolved significantly in Bash 2.0+ with expanded -A actions and -o options. Core to Bash's completion system, maintained through Bash 5.x for enhanced scripting and interactivity.


