Properly handling spaces and quotes in bash completion

后端 未结 5 1985
悲&欢浪女
悲&欢浪女 2020-12-05 06:54

What is the correct/best way of handling spaces and quotes in bash completion?

Here’s a simple example. I have a command called words (e.g., a dictionar

5条回答
  •  -上瘾入骨i
    2020-12-05 07:41

    _foo ()
    {
      words="bar one"$'\n'"bar two"
      COMPREPLY=()
      cur=${COMP_WORDS[COMP_CWORD]}
      prev=${COMP_WORDS[COMP_CWORD-1]}
      cur=${cur//\./\\\.}
    
      local IFS=$'\n'
      COMPREPLY=( $( grep -i "^$cur" <( echo "$words" ) | sed -e 's/ /\\ /g' ) )
      return 0
    }
    
    complete -o bashdefault -o default -o nospace -F _foo words 
    

提交回复
热议问题