#StackBounty: #bash #autocomplete #complete Escape slashes in bash complete

Bounty: 250

I try to use the bash complete builtin to show different options for a command.

I have problems when an option contains a path like in -F/dev/null.

Currently I’m using

#!/bin/bash

_xyz-completion ()
{
  local cur

  COMPREPLY=()   # Array variable storing the possible completions.
  cur=${COMP_WORDS[COMP_CWORD]}

  case "$cur" in
    -*)
    COMPREPLY=( $( compgen -W "-oOption1 -F/dev/null" -- $cur ) )
    ;;
  esac

  return 0
}

complete -F _xyz-completion -o filenames xyz

If -F was already typed, then a Tab completes it successfully.

But if only - was typed, then a Tab shows

null       -oOption1

But I expect to see

-F/dev/null       -oOption1

I tried already -F/dev/null, -F//dev//null, "-F/dev/null" and -F\/dev\/null

It seems to be only a display problem, as the completion itself works as expected.

I can’t see how to appropriate escape the slashes in `-F/dev/null`.


To comment the comments:

1)

Never mind, it’s a problem also if -F is replaced by a non-option such as -Q. – Benjamin W.

It’s not a problem, that the -F looks like a option for complete itself, as it even fails if I changed it to xOPTION1 xF/dev/null

2)

I’m wondering what compgen -W “-oOption1 -F/dev/null” — – displays for you.

It displays (as expected)

-oOption1
-F/dev/null

As mentioned, -F completes successfully to -F/dev/null


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.