4

I'm trying to setup a bash completion, and have two problems

  1. For parameter one, I needed a completion for directories
  2. For parameter two, a fixed array for completion, I just don't know how to let bash make a selection, before I always used a perl script for complex ones.
_some_func()
{
    case $COMP_CWORD in
    1)
        # default completion ? how
        ;;
    2)
        COMPREPLY=( "go" "unbind" )
        # I should be using a program to echo "go" and "unbind",
        # and let bash decide which one to complete , right ? 
        # that's the only two possible parameters here
        ;;
    esac
}

complete -F _some_func some_func

1 Answer 1

5

Here's one way to do it: set dirnames as the default completion, and generate custom completions for the second argument.

_some_func () {
  case $COMP_CWORD in
    1) :;; # let the default take over
    2) COMPREPLY=($(compgen -W "go unbind" "${COMP_WORDS[$COMP_CWORD]}"));;
    *) COMPREPLY=("");;
  esac
}
complete -F _some_func -d some_func

You could also call compgen -d when $COMP_CWORD is 1, but this doesn't work well in bash because you need to escape whitespace in the output of compgen, and you can't distinguish a newline that separates two results from a newline that is contained in a completion (rare, but possible).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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