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 jzb@zonker.net and follow him on Twitter.

Follow ServerWatch on Twitter

Latest Posts

Get-MsolUser PowerShell Attributes & Properties

This article has been updated for 2020. Please note that WAAD was retired in 2018, but the cmdlets listed in this article are still...

Microsoft Azure PowerShell Scripts and Commands

Using PowerShell scripts and commands for quickly executing tasks in Windows operating systems offers a number of benefits over traditional scripting languages, such as...

Best Cloud Based Services & Companies

Any company that’s delayed introducing cloud-based software into their infrastructure needs to consider leveraging these new technologies to reap all the benefits cloud computing...

Best Load Balancers for 2020

The best load balancers are able to provide either hardware or software-based load balancing to ensure maximum service availability by offering network traffic distribution...

Best Enterprise Database Software & Systems for 2020

If you're looking for a system to store your company’s data on-premises or in the cloud, chances are you'll be considering at least one...

Related Stories