0

I am playing around with the programmable completion of Bash and there is something I don't quite understand.

Environment

This is what I get when I run bash --version in my terminal.

GNU bash, version 5.1.16(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

I am running all this in an Ubuntu 22.04.2 LTS

I have two files.

FILE 1 (The script)

The first one is the script for which I want to have programmable completion which looks like this:

#!/usr/bin/env bash
#Script: dothis
echo "COMMAND: $0 $@"

As you can see this is a dummy script that prints the name of the script followed by the arguments.

FILE 2 (Programmable Completion Script)

The second file, which is the file in charge of the programmable completion for the previous script, looks like this:

#!/usr/bin/env bash

_dothis_completions(){
    # -S suffix
    COMPREPLY=($(compgen -A user -S '_suffix'  "${COMP_WORDS[1]}"))

    # -P prefix
    #COMPREPLY=($(compgen -A user -P 'prefix_'  "${COMP_WORDS[1]}"))
}

 complete -F _dothis_completions dothis

When I use it with the option -S (to attach a suffix to the suggestions) it works perfectly. It gives me as suggestions the different users in the system with _suffix attached at the end.

Now, if I modify the script for programmable completion to look like this:

#!/usr/bin/env bash

_dothis_completions(){
    # -S suffix
    #COMPREPLY=($(compgen -A user -S '_suffix'  "${COMP_WORDS[1]}"))

    # -P prefix
    COMPREPLY=($(compgen -A user -P 'prefix_'  "${COMP_WORDS[1]}"))
}

 complete -F _dothis_completions dothis

(Please notice that now I want to use the -P flag to attach the prefix prefix_ to all suggestions)

And I try it again in the command line, it does not work.

Does anyone know why this is not working?

Thanks a lot in advance!

Update (July 29th 2023)

I decided to use the programmable completion of Bash from Zsh and seems to be working without issues.

Hope this bit of information will help us find the answer to why the "-P" flag does not work properly in Bash.

1
  • I think the syntax for -P option has no quotes. Try compgen -A user -P prefix_ Jun 30, 2023 at 19:05

1 Answer 1

1

The problem is that compgen -P ... seems to make sense only if there is only one option left. Maybe you could fix this problem by removing the prefix when generating completions.

My suggestion is to check whether anything has been typed and whether there is only one completion left. This will probably not work with two values of which one is the fist part of the other like

  • foo
  • foobar

You could not select foo then.

_dothis_completions(){

    local -a tmp_array=()

    # -S suffix
    #COMPREPLY=($(compgen -A user -S '_suffix'  "${COMP_WORDS[1]}"))

    # -P prefix
    if [ -z "${COMP_WORDS[COMP_CWORD]}" ]; then
        COMPREPLY=($(compgen -A user "${COMP_WORDS[1]}"))
    else
        tmp_array=( $(compgen -A user "${COMP_WORDS[1]}") )
        if [ "${#tmp_array[@]}" -eq 1 ]; then
            COMPREPLY=($(compgen -A user -P 'prefix_'  "${COMP_WORDS[1]}"))
        else
            COMPREPLY=($(compgen -A user "${COMP_WORDS[1]}"))
        fi
    fi
}

complete -F _dothis_completions dothis

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .