0

When I define an environment variable inline like this: env_variable=true command, my custom bash completion script fails. Example:

user@host in /tmp/tmp.6DRvQAkpFe
> ls
user@host in /tmp/tmp.6DRvQAkpFe
> docker-ssh 
container1 container2 container3 puppetmaster           
user@host in /tmp/tmp.6DRvQAkpFe
> VARIABLE=TEST docker-ssh 

The last command doesn't autocomplete like the one before it.

My bash completion function looks like this:

_docker-ssh() {
    local running=$(docker ps --format '{{.Names}}') 
    local cur prev opts base

    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"

    COMPREPLY=($(compgen -W "${running}" -- ${cur}))

    return 0
}

complete -F _docker-ssh docker-ssh

Is there something I'm missing in my COMPREPLY that would take into account environment variables?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
Carey
  • 650
  • 1
  • 4
  • 16
  • 1
    I cannot reproduce this issue in any way. Use `set -x` inside the function to check the actual values. – PesaThe Jul 25 '18 at 09:06
  • I can't reproduce it either. As a side note, you don't need your function, you can reduce the second code block to `complete -W '$(docker ps --format "{{.Names}}")' docker-ssh`. – Benjamin W. Jul 25 '18 at 14:25

1 Answers1

0

The behavior you want is a feature introduced in bash 4.3.

The command completion code skips whitespace and assignment statements before looking for the command name word to be completed.

Grisha Levit
  • 8,194
  • 2
  • 38
  • 53