Custom path completion in Bash
<p>I want to write a bash_completion script for my own file system. I have client program which sends queries to some DB.</p>

<p>example:</p>

<pre><code>my_prog --ls db_name:/foo/bar/
</code></pre>

<p>this command writes to stdout list of files in <code>db_name:/foo/bar</code> folder.</p>

<p>I want to enable autocompletion for this. So when i press tab it shows list of options.</p>

<pre><code>my_prog --ls db_name:/foo/bar/&lt;tab&gt;
</code></pre>

<p>but in this case when I press tab and there is single option it replaces the current entered path, so I'm getting this:</p>

<pre><code>$ my_prog --ls db_name:/foo/bar/&lt;tab&gt;
$ my_prog --ls file
</code></pre>

<p>But I want the match to be added to end of entered path.</p>

<p>Here is my completion function:</p>

<pre><code>__complete_path()
{
    COMPREPLY=()

    if [[ ${1} == "" ]]
    then
        COMPREPLY=( "/" )
        compopt -o nospace
        return
    fi

    base=${1##*/}
    dir=${1%/*}

    options="my_prog --ls ${db}:${dir}"
    COMPREPLY=( $(compgen -W "${options}" -- ${base} ) )

    compopt -o nospace
}
</code></pre>