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

Say I have a program hello.py and one possible valid argument to it is:

./hello.py autoawesomesauce

Is is possible to type in:

./hello.py auto[tab]

at which point the partially completed argument is sent to hello, which recognizes it as such and then completes it on the shell to:

./hello.py autoawesomesauce

I know git does something like this, but can it be done for a Python script + Bash?

share|improve this question

migrated from stackoverflow.com Apr 22 '11 at 15:02

This question came from our site for professional and enthusiast programmers.

The Python script can't. bash must. – Ignacio Vazquez-Abrams Apr 22 '11 at 14:57
upvote for what exactly? – RestRisiko Apr 22 '11 at 14:58

On Linux systems, you can generally find a large number of example scripts under: /etc/bash_completion.d. If you source these scripts, then you will get the autocomplete behavior.

I've included an example from that directory. This is the completion script for unrar.

    local cur

    _get_comp_words_by_ref cur

    if [[ "$cur" == -* ]] ; then
        COMPREPLY=( $( compgen -W '-ad -ap -av- -c- -cfg- -cl -cu \
            -dh -ep -f -idp -ierr -inul -kb -o+ -o- -ow -p -p- -r -ta \
            -tb -tn -to -u -v -ver -vp -x -x@ -y' -- "$cur" ) )
        if [ $COMP_CWORD -eq 1 ]; then
            COMPREPLY=( $( compgen -W 'e l lb lt p t v vb vt x' -- "$cur" ) )
            _filedir '@(rar|RAR)'

} &&
complete -F _unrar -o filenames unrar
share|improve this answer

This functionality has zero to do with Python. This is a pure functionality of the underlaying shell. So read up the bash docs on auto-completion.

Google for "bash auto-completion" and you will find at least 5 reasonable docs under the first 10 hits.

share|improve this answer
Which means it may be a good idea to wrap your Python script in a Bash script, letting the Bash script do as much argument handling/parsing as possible. – Kazark Sep 11 '12 at 18:31

See this related StackOverflow post.

complete 'your_command' 'p/*/`echo list_of_your_options`/'
share|improve this answer

Your Answer


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