Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

compgen Command in Linux



compgen is a Linux command that allows you to list all commands that you can execute on a Linux system. It is primarily used for autocompletion in the terminal. You can type a command or argument partially and press the TAB key to autocomplete it.

compgen generates a list of possible completions, including commands, aliases, functions, and more. For example, you can use it to find all available commands or count the total number of commands in the terminal.

Table of Contents

Here is a comprehensive guide to the options available with the compgen command −

How to Install compgen Command in Linux?

The compgen is a built-in feature in the bash shell, thus, you dont need to separately install it, as it is available by default. It is also important to keep in mind that compgen is not an executable program, but rather a part of the shell itself. When using it, you need to invoke the shell (usually bash) to execute the command.

Syntax of compgen Command

The basic syntax to use the compgen command in Linux is provided below −

compgen [options] [word]

You can replace [options] with specific flags, while the [word] is options that allows you to filter results based on a specific keyword.

Common Options with compgen Command

Here, are some of the common options that you can use with the compgen command in your Linux system −

Option Description
-A function List all Bash functions.
-a List all Bash aliases.
-b List all Bash built-in commands.
-c List all available commands.
-k List all Bash keywords.

Examples of compgen Command in Linux

Lets explore some common examples of compgen command in Linux system −

  • List All Commands in Linux
  • Search for Commands with a Specific Keyword
  • List Bash Aliases
  • List Bash Built-ins
  • List Bash Keywords
  • List Bash Functions

List All Commands in Linux

If you want to list all available commands on your Linux system, you can use the compgen command followed by the -c option, as given below −

compgen -c
List All Commands in Linux

The above command will provide you with the auto completion options and allow you to explore various commands, aliases and functions.

Search for Commands with a Specific Keyword

You can also modify the compgen command in such a way that you will get the commands with a specific keyword. For example, to search for a commands with a keyword network, use the below-given command −

compgen -c | grep network
Get the Commands with a Specific Keyword

List Bash Aliases

If you want to list all defined bash aliases on your Linux system, you can use the compgen command with the -a option, as provided below −

compgen -a
List Bash Aliases

List Bash Built-ins

To view all built-in commands provided by Bash on Linux, simply use the compgen command with -b option, which is given below −

compgen -b
List Bash Built-ins

List Bash Keywords

Bash keywords are the reserved keywords that have special meaning within the shell. You can list bash keywords by using compgen command with -k option. The command for such a process is given below −

compgen -k
List Bash Keywords

List Bash Functions

Bash functions are reusable blocks of code within a Bash script or shell session that allow you to organize and encapsulate specific tasks or operations. You can list bash functions on your Linux system by using the -A option followed by the function keyword with the compgen command, as provided below −

compgen -A function
List Bash Functions

Thats how you can use the compgen command to list executable commands in your Linux system.

Conclusion

compgen is a powerful tool for exploring available commands, aliases, and functions on your Linux system. This tutorial covered in detail how you can use the compgen command in Linux, starting with the installation method followed by its syntax and different options that can be used with the compgen command.

Advertisements