Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When using commands in bash I like the double tab option to display the available commands. Some commands have more possible matches than others:

Screenshot of a tab completion

Is there a way I can pipe the output of the double tab to somwehere, like grep? I found a related post, but I'm still not sure how to implement it to pipe it to grep.

share|improve this question
How exactly do you imagine you would enter the grep regex? (y or n or g[rep])? I hate to sound pessimistic, but I doubt this could be done without adding it to the bash code. – Kevin Nov 3 '11 at 17:41
Related question – l0b0 Feb 14 '12 at 14:10
up vote 16 down vote accepted

For commands use compgen -c:

$ compgen -c bas

This output you can simply pipe through grep.

share|improve this answer
+1 For the command... Next interesting part could be how to make a keyboard shortcut for this. – rozcietrzewiacz Nov 4 '11 at 11:17

Based on @salutis's answer I created a script which I called comp and stored in ~/bin/comp that searches commands', aliases, and builtins, (option flag -cab see the bash man entry), with an optional second parameter which, if present, pipes the output to grep and searches for the second parameter.

Usage: comp string [keyword-for-grep]



if [ -z "$1" ]; then
    echo usage: comp string [keyword-for-grep]

if [ -z "$2" ]; then
    compgen -cab -- $1

compgen -cab -- $1 | grep -i $2

Personally I would also be interested in figuring out a way to remove the last command from the shell history in the script (something related to history -d) so that when searching bash history I won't find comp entries. I know I can also do this with HISTIGNORE but linux is pretty powerful so there must be a way to do it from the script file, too - right?

share|improve this answer
Of course you can adjust this to suit your needs, but it should get you started. – mark Dec 4 '11 at 20:00

Why not find what you are looking for?


find / -name ec2* -executable -type f -perm -og+rx -print

Knowing your use case I can create a more detailed command example.

share|improve this answer
In other words you can reimplement whatever bash's completion does. Sure, but that's not practical, and it's not what the question is about. – Gilles Nov 3 '11 at 23:27

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.