Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
Also see unix.stackexchange.com/questions/23900/… – cwd Dec 31 '12 at 2:50

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 ...
share|improve this answer
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
@Gilles: thank you. – enzotib Sep 28 '11 at 7:04

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.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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