Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
In Bash, it’s possible to autocomplete commands or arguments by typing them partially and pressing the tab key. Further, we can use the compgen built-in command to retrieve all possible options for autocompletion in the shell. This includes files, command aliases, builtins, etc.
In this tutorial, we’ll look at how to use compgen to obtain a list of completion options for a given input string. All of the examples we’ll see involve printing output to the shell. However, in a real-life scenario, we’re more likely to use the compgen command inside another program or script.
Let’s start by looking at the help output for the command:
$ help compgen
compgen: compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat] [-W wordlist] [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
Display possible completions depending on the options.
Intended to be used from within a shell function generating possible
completions. If the optional WORD argument is supplied, matches against
WORD are generated.
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.
The output tells us that we can use the command in the most basic form as compgen -[options] [word], where options can be any of abcdefgjksuv. Obviously, each of the characters has a special meaning:
We can use one or more of these options as we need.
Further, let’s see some examples below:
$ compgen -v XDG
XDG_CONFIG_DIRS
XDG_CURRENT_DESKTOP
XDG_DATA_DIRS
XDG_GREETER_DATA_DIR
XDG_MENU_PREFIX
XDG_RUNTIME_DIR
...
$ compgen -df Fira
FiraCode75-Regular.otf
Fira_Code
Fira_Code
First, we used the option -v to look for variables starting with XDG. Subsequently, we used the options d and f to look for files or directories starting with Fira in the current directory. Since the f option also includes directories, we see that the directory Fira_Code is displayed twice, once from the d option and once from the f option.
The compgen command also gives us the ability to specify other arguments such as -A, -G, -W, -F, -C, -X, -P, and -S. Let’s see what each of these can do:
Next, let’s see what some of these arguments do:
$ compgen -f -G *.otf
Inconsolata for Powerline.otf
FiraCode75-Regular.otf
$ compgen -d -P dir-
dir-Monoid-XtraLarge-Dollar
dir-Roboto_Mono
dir-Fira_Code
dir-Jost
dir-Source_Code_Pro
dir-powerline-fonts
$ compgen -W 'mon tue wed thu fri sat sun' s
sat
sun
In the above three examples, firstly we see that the -G option matches using a pattern to complete. Subsequently, the -P option outputs the options with the given prefix. Lastly, the -W uses the word list supplied for generating the completions.
In this article, we looked at different ways to use the compgen command to generate completions for a given word. Thus, we can use the command in other programs and Bash scripts for generating custom completions.