compgen command in Linux with Examples
Last Updated :
09 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

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

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

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

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

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

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

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.
Similar Reads
cpp command in Linux with Examples cpp is the C language preprocessor, it is automatically used by your C compiler to transform your program before compilation. It is also termed as a macro processor because it is used to give abbreviations for the longer piece of code. It can only be used with C, C++ and Objective-C source code. Usi
3 min read
env command in Linux with Examples The 'env' command in Linux is a versatile tool used to manage environment variables. It can print all the current environment variables, set new ones, or run a command within a custom environment. Additionally, 'env' is commonly used in shell scripts to launch the correct interpreter, ensuring the s
3 min read
ex command in Linux with examples ex (stands for extended) is a text editor in Linux which is also termed as the line editor mode of the vi editor. This editor simply provided some editing commands which has greater mobility. With the help of this editor, a user can easily move between files. Also, he/she has a lot of ways to transf
4 min read
echo command in Linux with Examples The echo command in Linux is a built-in command that allows users to display lines of text or strings that are passed as arguments. It is commonly used in shell scripts and batch files to output status text to the screen or a file. Syntax of `echo` command in Linuxecho [option] [string]Here, [option
3 min read
dc command in Linux with examples The dc command is a versatile calculator found in Linux systems, operating using reverse Polish notation (RPN). This command allows users to perform arithmetic calculations and manipulate a stack, making it ideal for complex mathematical tasks directly from the command line.SyntaxThe basic syntax fo
3 min read