compgen command in Linux with Examples (original) (raw)

Last Updated : 9 Oct, 2024

The **compgen command is a Bash built-in utility used to list all available commands that can be executed in a Linux system. It is a powerful tool for searching for commands based on specific keywords, counting the total number of commands, and printing Bash details such as built-in functions, keywords, and aliases. This article explores the various uses of the compgen command in Linux, along with examples.

Syntax

compgen [option]

The command is typically used with various options to filter and display specific types of commands or Bash elements.

Common Use Cases and Options for the compgen Command

**1. List All Available Commands

To list all commands available to be directly executed.

compgen -c

To-list-all-commands-available-to-be-directly-executed

This will list all the commands available to you for direct use.

**2. To search for commands having a specific keyword

To search for commands containing a specific keyword, you can combine the compgen command with grep. For example, to search for commands containing the keyword "gnome".

compgen -c | grep gnome

To search for commands having a specific keyword

This will search the commands having the keyword gnome in them.

**3. To count total number of commands available for use

To count the total number of commands available in the system, pipe the output of compgen **-c to **wc - l

compgen -c | wc -l

To count total number of commands available for use

This command will count the total number of commands that could be used.

**4. To list all the bash alias

To list all the Bash aliases present in your system, use the -a option

compgen -a

To list all the bash alias

This command will list all the bash alias present in your system.

**5. To list all the bash built-ins

List all Bash built-in commands, use the -b option

compgen -b

To list all the bash built-ins

This command will list all the bash built-ins present in your system.

**6. To list all the bash keywords

List all Bash keywords available in the system, use the **-k option

compgen -k

To list all the bash keywords

This command will list all the bash keywords present in your system.

**7. To list all the bash functions

List all Bash functions present in your system, use the -A function option

compgen -A function

To list all the bash functions

This command will list all the bash functions present in your system.

Conclusion

The compgen command is a versatile and useful tool in Bash for listing and searching various commands, aliases, built-ins, keywords, and functions in the Linux system. With the ability to filter and count commands, it can assist in better understanding and utilizing your command-line environment.