Working in complex Linux terminal environments often leaves developers and sysadmins wondering which commands, aliases, or shell functions are currently accessible in their active execution environment. While commands like which or type -a inspect a specific known program name, they cannot generate comprehensive listings of all executable assets.
Bash contains an extraordinarily powerful built-in tool called compgen (short for completion generator). As the underlying engine powering Bash’s programmable tab-completion subsystem, compgen allows you to query, filter, and inspect every command, alias, function, keyword, and shell variable available to your interactive shell or automation scripts.
Listing All Available Executable Commands
To print every command available in your current environment (including binary executables located throughout your $PATH, shell builtins, and functions):
compgen -c
Because modern Linux distributions include thousands of binaries, pipe the output through a pager or filter:
# Count total available commands
compgen -c | wc -l
# Search for all commands starting with "systemd"
compgen -c systemd
Filtering by Specific Asset Types
The true power of compgen lies in its granular action flags (-A or short options), which isolate specific shell entities:
1. Listing Shell Builtin Commands
Shell builtins (such as cd, echo, read, and source) execute directly within the Bash process rather than spawning external binary processes. To list all builtins:
compgen -b
2. Listing Shell Aliases
To inspect all shorthand aliases defined across your ~/.bashrc, /etc/bash.bashrc, and profile configurations:
compgen -a
3. Listing User-Defined Shell Functions
To output the names of all custom functions loaded in your current shell session:
compgen -A function
4. Listing Reserved Bash Keywords
To inspect reserved programming keywords (such as if, then, elif, while, case, and for):
compgen -k
5. Listing Environment and Shell Variables
To display all defined variable names without printing their associated sensitive values or payloads:
compgen -v
Matching Commands by Prefix
You can append any search prefix directly to a compgen invocation. This acts identically to pressing Tab in the terminal, but dumps the complete programmatic list to stdout:
# List all commands beginning with "net"
compgen -c net
# List all aliases beginning with "g" (e.g. git shortcuts)
compgen -a g
# List all functions beginning with "deploy_"
compgen -A function deploy_
Practical Shell Scripting Applications
Testing If a Command or Function Exists
Rather than parsing the output of which (which behaves inconsistently across different operating systems and shells), use compgen -c inside an automated validation function:
command_exists() {
compgen -c "$1" | grep -qx "$1"
}
if command_exists docker; then
echo "Docker engine is detected and ready."
else
echo "Docker binary missing from system PATH." >&2
exit 1
fi
Building Custom Dynamic Autocompletion
When authoring bespoke CLI scripts or tools, wrap compgen inside the standard complete builtin to provide autocomplete candidates for subcommands:
_mycli_complete() {
local cur="${COMP_WORDS[COMP_CWORD]}"
local subcommands="build deploy status destroy logs"
COMPREPLY=( $(compgen -W "$subcommands" -- "$cur") )
}
complete -F _mycli_complete mycli
Mastering compgen unlocks rapid command-line introspection and forms the cornerstone of professional Bash shell scripting and custom CLI development.