Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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 12 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 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

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 : http://www.debian-administration.org/articles/316

share|improve this answer

Here is a complete tutorial.

Lets have an example of script 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/script.sh shortlist)

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

  return 0
complete -o nospace -F _admin ./admin.s

All you need to do is to add your autocomplete script to bashrc as

source /path/to/your/autocomplete.sh

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

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.