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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
For commands use compgen -c:
$ compgen -c bas basename base64 bashbug bash basename base64 bashbug
This output you can simply pipe through grep.
Method 2
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?
Method 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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0