Is there a nice way to print (e.g. to stdout) a list of all possible completions of a command in bash (the ones it would give me if I typed the command, then pressed tab)?

Edit: I guess this wasn't really clear, now that I read it again. I know how to get completions manually. I want to do it programmatically.

link|improve this question
feedback

5 Answers

up vote 3 down vote accepted

bash does not have a function to do exactly that. The programmable completions depend heavily on various parameters such as the cursor position...

eval $(complete -p 'git' | sed s/^complete/compgen/)

seems to work, but is very fragile.

link|improve this answer
This isn't quite exactly what I wanted, but I guess it's probably as close as I'll get. – Ben Kraft Aug 21 '11 at 15:39
feedback

Press TAB twice. Most BASH-like shells will list all matching auto-completion options for a given substring.

e.g.

root@host$apt-get up

pressing TAB twice on this command will list both Update and Upgrade

link|improve this answer
feedback

Make sure you have the bash-completion package installed. Depending on your install type it may or may not be included by default, if not just grab it with yum or apt. After installing, you'll need to log out and log back in or re-source your .bashrc to use it.

@Amazed is spot on as for how to use it.

link|improve this answer
feedback

Not really... remember that the completions can be a function and can be dependent on previous arguments.

That said, complete | grep ... might get you someplace

link|improve this answer
feedback

I think what you're asking for is basically a list of all the commands beginning with a given string. You should be able to do that by getting a list of all the executables on your system and running them through grep.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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