Guides A Look at the Compgen Bash Builtin

A Look at the Compgen Bash Builtin




Many features that come with Linux and other Unix-type systems are non-obvious and almost indiscoverable. To put it another way, unless someone points out a feature or you RTFM a lot, you’ll miss a wealth of features under the surface. One feature I’d been missing for years is compgen, a GNU Bash builtin that shows all possible completions.

To get everybody up to speed–when I say completions, what I mean is command names, paths, and so on, that might be completed when given a part of a string. For example when you’re typing a command and start with “apr”, you could get apropos on an Ubuntu system. To figure this out using compgen you could use compgen -c | grep apr. To quickly figure out all directories, you could cd to in a directory, just use compgen -d. (Or compgen -d | sort if you want a neat list…)

Note that compgen -d is not limited to the present working directory. Run compgen -d /etc/ for example, (or use in a script), and you get a full list of directories under /etc. The closing / is necessary, by the way.

The compgen utility is often used in scripts, but it can also be handy just to figure out what commands are installed on a system. Run compgen -c, and you’ll get a full list of all commands. Run compgen -a, and you should see any system-wide aliases.

You can also get all variable names using compgen like so:

compgen -v

Curious how you’d use this in a script? There’s a good post by Adam Backstrom about using Bash’s programmable completion in a script.

Wondering what commands have keybindings? Use compgen -A binding.

Note that compgen works hand in hand with the complete builtin. Both complete and compgen could do with much better documentation.

But if you’ve been working with Bash and never used compgen, now’s a good time to take a look. It’s a really useful little utility/builtin to have at your fingertips.

Joe ‘Zonker’ Brockmeier is a freelance writer and editor with more than 10 years covering IT. Formerly the openSUSE Community Manager for Novell, Brockmeier has written for Linux Magazine, Sys Admin, Linux Pro Magazine, IBM developerWorks, Linux.com, CIO.com, Linux Weekly News, ZDNet, and many other publications. You can reach Zonker at [email protected] and follow him on Twitter.

Follow ServerWatch on Twitter

Latest Posts

How to Convert a Physical Computer to a Virtual Machine

Many organizations are implementing virtualization technology into their networks to convert physical computers to virtual machines (VM). This helps reduce overall physical hardware costs,...

HPE ProLiant DL380 Gen10: Rack Server Overview and Insight

The HPE ProLiant DL380 series has consistently been a market leader in the server space. The Gen10 released in 2017 further increased HPE's market...

Best Server Management Software & Tools 2021

Finding the best server management software tools for your organization can have a major impact on the success of your business operations. Manually handling...

IBM AS/400: Lasting the Test of Time

Some server operating systems (OS) were built to survive the test of time – the IBM AS/400 is one such system.  The AS/400 (Application System/400)...

What is Disaster Recovery?

The modern organization's heavy dependence on using data to drive their business has made having a Disaster Recovery (DR) plan in place a necessity....

Related Stories