Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

4 Answers 4

up vote 14 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 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 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
Slight bug in the autocomplete script provided. The complete function is called with the argument _admin, but the script defined the function _script. This causes an error in the completion. –  Rob Hall Aug 24 '14 at 18:04
@RobHall comment is correct, the last line should read complete -o nospace -F _script where the last argument is the script you are trying to add autocompletion to i e if the first script is called I've edited the answer. –  jonasl Apr 12 at 7:44

I believe if you're script file is executable, you can do tab completion in terminal.

share|improve this answer
The OP is asking to make the options of his script auto-complete on the press of the tab key. Making the script executable, will only make the script name "auto-completable" –  Dan Nov 11 '13 at 17:31
that's not quite true. It will autocomplete filenames and directories as arguments to the script once it has executable positions. However, this answer will not fulfil the OP's example. –  sillyMunky Dec 21 '13 at 2:11

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.