41

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?

Braiam
  • 35,991
Falmarri
  • 13,047

2 Answers2

45

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").

Ahatius
  • 103
Steven D
  • 46,160
16

Here is basic guide.

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

#!/usr/bin/bash

while [ $# -gt 0 ]; do arg=$1

case $arg in option_1) # do_option_1 ;; option_2) # do_option_1 ;; shortlist) echo option_1 option_2 shortlist ;; *) echo Wrong option ;; esac

shift done

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:

#!/usr/bin/bash

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

local cur prev COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" 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... – searchengine27 Jul 10 '17 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 09 '23 at 20:03
  • @joshtch Post updated. May be you will be interested on https://github.com/reduardo7/bashx too – Eduardo Cuomo Apr 11 '23 at 17:30