1

Let's say I have a simple script in /usr/local/bin/myscript like

#!/bin/bash

case "$1" in
    start)
        start something
        ;;
    stop)
        stop something
        ;;
    status)
        status of something
        ;;
    *)
      echo "unknown option $1"
esac

How and where could I provide those three options start/stop/status so a user can display/autocomplete them pressing TAB?

Like e.g. apt-get + 2x Tab gives me

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

EDIT

As sugested by @Ravexina I added a file in /etc/bash_completion.d/myscript_comp like

_my_script_comp ()
{
  local cur # A pointer named "cur" to current completion word.
  COMPREPLY=() # Array variable storing the possible completions.
  cur=${COMP_WORDS[COMP_CWORD]}

  # Show it for every possible combination
  # we could do "s*" to only complete words starting with "s"
  case "$cur" in
    # Generate the completion matches and load them into $COMPREPLY array.
    *)  COMPREPLY=( $( compgen -W 'start status stop' -- $cur ) );;
  esac

  return 0
}

complete -F _my_script_comp myscript

But whn I type myscript and press 2x TAB I now get all files listed in the current directory instead of start stop status...

  • Brilliant... I have been using Ubuntu for a considerable time now and it never occurred to me apt-get has this option. I always used -h. – user680858 Jun 30 '17 at 9:45
  • @WillemK it's not only apt-get but the whole ubuntu terminal providing the autocompletion and suggestions by pressing TAB ;) – derHugo Jun 30 '17 at 9:46
  • I know, I just never considered to use it with apt-get, cause most of its commands are typed in almost automatically. :) – user680858 Jun 30 '17 at 9:54
2

Create a file here:

/etc/bash_completion.d/

name it whatever you want, e.g: myscript.

Add these lines into it and save it:

_my_script_comp ()
{
  local cur # A pointer named "cur" to current completion word.
  COMPREPLY=() # Array variable storing the possible completions.
  cur=${COMP_WORDS[COMP_CWORD]}

  # Show it for every possible combination
  # we could do "s*" to only complete words starting with "s"
  case "$cur" in
    # Generate the completion matches and load them into $COMPREPLY array.
    *)  COMPREPLY=( $( compgen -W 'start status stop' -- $cur ) );;
  esac

  return 0
}

complete -F _my_script_comp script.sh

Now you've got your bash completion for /usr/local/bin/script.sh.

My source.

  • Hi thanks so far! Please have a look at my Edit. Unfortunately it is still not working – derHugo Jun 30 '17 at 9:48
  • 1
    You might want to source the file, run source /etc/bash_completion.d/* – Sergiy Kolodyazhnyy Jun 30 '17 at 9:56
  • I've updated the answer ... check it out again. – Ravexina Jun 30 '17 at 9:56
  • And as @SergiyKolodyazhnyy suggest either source the script or open a new terminal to see if it works or not. – Ravexina Jun 30 '17 at 9:57
  • I applied your changes and did what @SergiyKolodyazhnyy said but it is still not working. Even after a reboot – derHugo Jun 30 '17 at 10:01

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.