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
ed command in Linux with examples Linux 'ed' command is used to start the ed text editor, a very minimal fronted line-based text editor. The lines are very simple in relation to other text files, hence easy to work on, for creating as well as editing, display, and manipulation of files. Since it was the first editor that was include
4 min read
doexec command in Linux with examples doexec command in the Linux system is used to run an executable with an arbitrary argv[0]. It allows the user to argv[0] other than the name of the executable, which is by default passed. Syntax: doexec /path/to/executable argv[0] [argv[1-n]] Options: The argv list is used to send all options to the
1 min read
comm command in Linux with examples The 'comm' command in Linux is a powerful utility that allows you to compare two sorted files line by line, identifying the lines that are unique to each file and those that are common to both. This command is particularly useful when you have lists, logs, or data sets that need to be compared effic
4 min read
emacs command in Linux with examples Introduction to Emacs Editor in Linux/Unix Systems: The Emacs is referred to a family of editors, which means it has many versions or flavors or iterations. The most commonly used version of Emacs editor is GNU Emacs and was created by Richard Stallman. The main difference between text editors like
5 min read
cc command in Linux with Examples 'cc' command stands for C Compiler, usually an alias command to 'gcc' or 'clang'. As the name suggests, executing the 'cc' command will usually call the 'gcc' on Linux systems. It is used to compile the C language codes and create executables. The number of options available for the cc command is ve
3 min read