Ask Ubuntu is a question and answer site for Ubuntu users and developers. 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

When I use program like svn and I type in Gnome Terminal:

svn upd

and hit Tab it's autocompleted to:

svn update

Is it possible to do something like that in my custom bash script?

share|improve this question
explain "bash script", you mean when editing a script? what do you want to do with it? – Bruno Pereira Oct 17 '11 at 15:25
when using script in console – UAdapter Oct 17 '11 at 15:29
up vote 13 down vote accepted

You can use the Programmable Completion. Have look at /etc/bash_completion and /etc/bash_completion.d/* for some examples.

share|improve this answer
How about including a simple example directly related to the question? – MountainX Jun 23 '13 at 21:14

I'm six months late but I was looking for the same thing and found this:

You'll have to create a new file:


For a static autocompletion (--help / --verbose for instance) add this:

    local cur prev opts
    opts="--help --verbose --version"

    if [[ ${cur} == -* ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
        return 0
complete -F _foo foo
  • COMP_WORDS is an array containing all individual words in the current command line.
  • COMP_CWORD is an index of the word containing the current cursor position.
  • COMPREPLY is an array variable from which Bash reads the possible completions.

    compgen -W "--help --verbose --version" -- "<userinput>"

This command returns the array of elements from --help, --verbose and --version matching the current word "${cur}"

Source :

share|improve this answer
This should be the accepted answer! It's a complete example. – Victor Schröder Aug 14 '15 at 11:21
Tip: If someone wants suggestions for words not starting with - and show them without having to start typing the target word, just remove the if [...] then and fi lines. – Cedric Reichenbach Sep 16 '15 at 9:23

Here is a complete tutorial.

Lets have an example of script called to which you would like to have autocomplete working.


while [ $# -gt 0 ]; do
  case $arg in
     # do_option_1
     # do_option_1
      echo option_1 option_2 shortlist
     echo Wrong option

Note option shortlist. Calling script with this option will print out all possible options for this script.

And here you have the autocomplete script:

  _script_commands=$(/path/to/your/ shortlist)

  local cur prev
  COMPREPLY=( $(compgen -W "${_script_commands}" -- ${cur}) )

  return 0
complete -o nospace -F _script ./

Note that the last argument to complete is the name of the script you want to add autocompletion to. All you need to do is to add your autocomplete script to bashrc as

source /path/to/your/

or copy it to /etc/bash.completion.d

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.