compgen: A built-in command for auto-completion in Bash, which is called on pressing TAB key twice

The “compgen” command is a built-in command in the Bash shell that is specifically designed for auto-completion purposes. It is invoked when you press the TAB key twice, triggering the shell to generate a list of possible completions based on the context.

When you activate auto-completion by pressing TAB twice, the Bash shell internally calls the “compgen” command. This command analyzes the current context, such as the command being typed or the arguments provided, and generates a list of possible completions.

The generated completions can include command names, file and directory names, variable names, options, or any other relevant suggestions based on the context. The “compgen” command essentially assists in speeding up the typing process by providing suggestions for the next possible input.

By default, the “compgen” command provides completions based on various sources, including built-in commands, aliases, environment variables, and filenames in the current directory. However, it can also be customized to include additional sources or exclude specific ones, depending on your preferences.

The “compgen” command supports different options and arguments to modify its behavior. For example, you can specify a particular completion type, filter the results, or restrict the completions to specific patterns or prefixes.

Auto-completion with “compgen” greatly enhances the efficiency and convenience of working in the Bash shell. It reduces the need to manually type lengthy commands or filenames by providing suggestions and completing them with a single keystroke.

It’s important to note that the availability and behavior of auto-completion may vary depending on the specific version of the Bash shell and the configuration settings. Different Bash configurations, plugins, or scripts may extend or modify the default behavior of “compgen” to provide additional features or customized completions.

compgen Command Examples

1. List all commands that you could run:

# compgen -c

2. List all aliases:

# compgen -a

3. List all functions that you could run:

# compgen -A function

4. Show shell reserved keywords:

# compgen -k

5. See all available commands/aliases starting with ‘ls’:

# compgen -ac ls

Summary

In summary, “compgen” is a built-in command in the Bash shell that is triggered when you press TAB twice for auto-completion. It generates a list of possible completions based on the current context, including commands, filenames, variables, and more. The command assists in speeding up typing by providing suggestions and completing input with a single keystroke. Auto-completion with “compgen” enhances efficiency and convenience when working in the Bash shell.

Related Post