5

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.

3

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.

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

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

2

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.

1

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

0

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.

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.