How to Find Commands/Aliases In Bash

The aim of this page📝 is to explain how to print and filter available commands in Bash.

Pavol Kutaj
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 compgen to list all available commands in Bash.
  • compgen -c lists all commands.
  • compgen -a lists all aliases.
  • compgen -b lists all built-ins.
  • compgen -k lists all keywords.
  • compgen -A function lists all functions.
  • Combine these options to list everything: compgen -c -a -b -k -A function.
  • Pipe the output through sort and uniq to 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 cmd from 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"
}
bash command discovery in action
  • Call the function with a regex pattern to find matching commands.
  • Example usage: cmd "my_custom" to find commands matching “my_custom”.

--

--

Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com