How to use the command 'compgen' (with examples)

How to use the command 'compgen' (with examples)

The ‘compgen’ command is a built-in command in Bash that is used for auto-completion. It is called by pressing the TAB key twice. This command provides various options to generate a list of commands, aliases, functions, or reserved keywords.

Use case 1: List all commands that you could run

Code:

compgen -c

Motivation:

This use case is helpful when you want to see a list of all available commands that you can run in the Bash shell.

Explanation:

The ‘-c’ option is used to specify the type of completion, in this case, ‘c’ for commands. It tells the ‘compgen’ command to generate a list of all commands.

Example Output:

apropos
apt
apt-cache
apt-cdrom
apt-config
...

Use case 2: List all aliases

Code:

compgen -a

Motivation:

Sometimes it’s useful to know all the aliases set up in the Bash shell, especially when you want to modify or remove existing aliases.

Explanation:

The ‘-a’ option is used to specify the type of completion, in this case, ‘a’ for aliases. It tells the ‘compgen’ command to generate a list of all aliases.

Example Output:

ll
lsa
l
..
...

Use case 3: List all functions that you could run

Code:

compgen -A function

Motivation:

If you have defined functions in your Bash shell or want to see all available functions, this use case comes in handy.

Explanation:

The ‘-A’ option is used to specify the type of completion, in this case, ‘A’ for functions. It tells the ‘compgen’ command to generate a list of all functions.

Example Output:

my_function1
my_function2
...

Use case 4: Show shell reserved keywords

Code:

compgen -k

Motivation:

Knowing the reserved keywords in the Bash shell helps to avoid naming conflicts and to understand the behavior of the shell.

Explanation:

The ‘-k’ option is used to specify the type of completion, in this case, ‘k’ for shell reserved keywords. It tells the ‘compgen’ command to generate a list of all the reserved keywords.

Example Output:

if
while
for
case
...

Use case 5: See all available commands/aliases starting with ’ls’

Code:

compgen -ac ls

Motivation:

When you want to find a specific command or alias starting with a specific string, you can use this use case to filter the list quickly.

Explanation:

The ‘-a’ option is used to generate a list of aliases, and the ‘-c’ option is used to generate a list of commands. By specifying both options, we get a list of both commands and aliases. The argument ’ls’ is used as a filter for the output.

Example Output:

ls
lsa

Conclusion:

The ‘compgen’ command is a powerful tool for auto-completion in Bash. It provides various options to generate lists of commands, aliases, functions, and reserved keywords. These use cases demonstrate how to use the command and its different options to retrieve specific types of information or filter the output. Whether you need to explore available commands or find a specific command/alias, ‘compgen’ can make your workflow more efficient.

Related Posts

Using the `hub branch` command (with examples)

Using the `hub branch` command (with examples)

Introduction The hub branch command is a useful tool for creating and managing branches in a GitHub repository.

Read More
How to use the command 'avrdude' (with examples)

How to use the command 'avrdude' (with examples)

The avrdude command is a driver program for programming Atmel AVR microcontrollers.

Read More
Using cpufreq-set (with examples)

Using cpufreq-set (with examples)

Setting the CPU frequency policy of CPU 1 to “userspace”: sudo cpufreq-set -c 1 -g userspace Motivation: This use case is helpful when you want to manually control the CPU frequency of a specific CPU core.

Read More