Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

This question is quite similar to this one, but I can't understand the solution. I also know this tutorial, but still I am unable to understand what I am doing wrong.

I am trying to autocomplete the list of directories that are placed in /something/:

$ ls /something/

One    Other    Three

in a way that this will happen:

$ hi [TAB]

One Other Three
$ hi O[TAB]

One Other

Only when the first word is hi and not only inside /something/.

This is what I am doing in .profile:

    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen `ls /something/` -- $cur) )

complete -F _codeComplete "hi "
share|improve this question
up vote 9 down vote accepted


    local cur=${COMP_WORDS[COMP_CWORD]}
    COMPREPLY=( $(compgen -W "$(ls /something/)" -- $cur) )

complete -F _codeComplete hi

You need to specify the -W option in compgen and also quote the command which produces the wordlist.

More information on how to write completion functions on this blog: Writing your own Bash Completion Function

share|improve this answer
Thanks a lot, this works perfectly. :-) – Donovan Jan 4 '12 at 14:36
This will fail on file names containing whitespace and other special characters. – Gilles Jan 4 '12 at 23:39
It is weirdly difficult to find the name of the complete command if you don't already know it and are searching for autocomplete or tab-completion... This was the first place I found the command name, and what I needed to fix it for eject on upgrade to Ubuntu 14.04 ($ complete -p ls -> complete -F _longopt ls, so the fix was: complete -F _longopt eject) (Here's hoping this comment helps others find it!) – Izkata Sep 1 '14 at 20:10

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.