Instead of displaying all possibilities on terminal screen, as in:

$ ls /etc/<TAB>
Display all 230 possibilities? (y or n)

I want to save all possibilities to a file.

ls /etc/ > file.txt will not always work. apt-get is an example:

$ apt-get <TAB>
autoclean        check            install          update
autoremove       clean            purge            upgrade
build-dep        dist-upgrade     remove           
changelog        dselect-upgrade  source           

I'm looking for a command like tabcompletions 'ls /etc/' which outputs all possibilities, so that I can run a command like the one below, which compares the tab completion possibilities for two commands:

diff <(tabcompletions 'ls ') <(tabcompletions 'cd ')

Is that possible?


In your ~/.bashrc you probably have something like this:

if [ -f /etc/bash_completion ] && ! shopt -oq posix
    source /etc/bash_completion

Now, that's where to continue looking, and the header of _quote_readline_by_ref contains the necessary hint:

compgen -f /etc/

Tracing this back, it turns out that (via type compgen) compgen is a "shell builtin", which means it should appear in man bash:

compgen [option] [word]
       Generate possible completion matches for word according to the options ...
  • An example using a completion function, as _apt_get would be interesting. – enzotib Sep 27 '11 at 15:59
  • @enzotib compgen -F _apt_get, except that it's not so simple: you would need to set a bunch of variables to emulate the real completion system. Zsh is even worse, the completion primitives (compadd, compset) only work if called from a completion widget, which in turns only works from the line editor, not from a script or even from a command line. – Gilles Sep 27 '11 at 22:23

Although, a crude method, you could use the command script

$ script -a lsdiff
Script started, file is lsdiff
$ ls <TAB>
a b c ...
$ <Ctrl-D>
Script done, file is lsdiff

Repeat the above for cd and compare the difference.

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.