11

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 http://img13.imageshack.us/img13/5541/picturemhy.jpg

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.

  • 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
20

For commands use compgen -c:

$ compgen -c bas
basename
base64
bashbug
bash
basename
base64
bashbug

This output you can simply pipe through grep.

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

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]

Code:

#!/bin/bash

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

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

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?

  • 1
    Of course you can adjust this to suit your needs, but it should get you started. – mark Dec 4 '11 at 20:00
-3

Why not find what you are looking for?

Example:

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

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

  • 2
    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 clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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