Is there a way to have bash know exactly what to display when you double tab? For example I have a python script scpy which requires a couple arguments. For example like apt-get, if you double tab gives you

autoclean        build-dep        clean            dselect-upgrade  purge            source           upgrade
autoremove       check            dist-upgrade     install          remove           update  

Is there a way to do that for your own scripts/programs? Do I need to wrap my python script in a bash script?


2 Answers 2


The easiest way of doing this is to include a shell script in /etc/bash_completion.d/. The basic structure of this file is a simple function that performs the completion and then invocation of complete which is a bash builtin. Rather than go into detail on how to use complete, I suggest you read An Introduction to Bash Completion. Part 1 covers the basics and Part 2 gets into how you would go about writing a completion script.

A denser description of bash completion can be found in the "Programmable Completion" section of man bash (you can type "/Programmable Completion" and then press 'n' a few times to get there quickly. Or, if you are feeling luck, "g 2140 RETURN").


Here is basic guide.

Lets have an example of script called admin.sh 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.sh script:


_script() {
  _script_commands=$(/path/to/your/admin.sh shortlist)

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

complete -o nospace -F _script /path/to/your/admin.sh

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 or /etc/bash_completion.d as:

source /full-path/to/your/autocomplete.sh

# or

. /full-path/to/your/autocomplete.sh

Finally, make them executable:

chmod a+x admin.sh autocomplete.sh

Source: https://askubuntu.com/a/483149/24155

  • 4
    'complete tutorial' is not the phrase I would use to describe this... Jul 10, 2017 at 16:09
  • I'm confused, is /path/to/your/script.sh the same file you called ./admin.sh later in the script? And what do you do if you didn't author the script, so you can't add a shortlist subcommand? This also doesn't really seem like standard practice.
    – joshtch
    Apr 9, 2023 at 20:03
  • @joshtch Post updated. May be you will be interested on github.com/reduardo7/bashx too Apr 11, 2023 at 17:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .