Q:

Custom bash completion with directories and a fixed set

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

For parameter one, I needed a completion for directories
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

A:

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

