How to Find Commands/Aliases In Bash
The aim of this page📝 is to explain how to print and filter available commands in Bash.
2 min readSep 20, 2024
compgen is a built-in Bash command that generates possible completion matches for a given string. It’s a powerful tool for listing commands, aliases, built-ins, keywords, and functions available in your shell. You can think of using compgen in Bash as a similar approach to using Get-Command and Get-Alias in PowerShell. Both sets of commands are used to list and discover available commands and aliases in their respective shells.
- Use
compgento list all available commands in Bash. compgen -clists all commands.compgen -alists all aliases.compgen -blists all built-ins.compgen -klists all keywords.compgen -A functionlists all functions.- Combine these options to list everything:
compgen -c -a -b -k -A function. - Pipe the output through
sortanduniqto sort and remove duplicates. - Use
grep -E "$regex"to filter commands based on a regex pattern. - Create a function to encapsulate this logic.
- Example function named
cmdfrom my.bash_profile - I use ripgrep (rg) instead of
grep…
function cmd() {
regex="$1"
compgen -c -a -b -k -A function | sort | uniq | rg "$regex"
}- Call the function with a regex pattern to find matching commands.
- Example usage:
cmd "my_custom"to find commands matching “my_custom”.
