I am attempting to use bash-completion for URLs of the form "proto://host". Without the ":", I see expected behavior, with the ":" present, the URL comes out "faulty".

Bash completion has a function for hostnames, _known_hosts_real. It has a parameter -p PREFIX that I want to use.

How to reproduce "wrong" behavior:

You need at least one "known" host on your system:

E.g. echo "gdns.google" >> /etc/hosts

Run these commands for functioning example:

$ unset COMPREPLY 
$ _known_hosts_real -p 'http//' -- "gdn"
$ echo $COMPREPLY 

Non-working example:

$ unset COMPREPLY 
$ _known_hosts_real -p 'http://' -- "gdn"
$ echo $COMPREPLY 

Note the only difference is the added colon in "http://". The problem here is the prefix has lost the protocol specifier and the colon. Is this expected behavior?


It is expected behaviour, as _known_hosts_real calls __ltrim_colon_completions, which removes prefixes terminated by colons. _known_hosts_real is intended for use with the ssh known hosts file, so you should probably define your own function on the basis of compgen -A hostname.

  • thanks for tracing that. In fact, I want the known ssh hosts to be included. So instead of writing something entirely new with basically the same function, I decided to add a patch to _known_hosts_real to my .bashrc, which adds options "-S" (suffix), and "-t" (dont trim). Jul 7 '16 at 17:15

@Michael_Veers pointed out, that this is expected behavior (+1), so I should write my own function.

But instead I wrote a patch for the standard _known_hosts_real into my bashrc, that enables additional options:

  • -t Do not trim to left colon
  • -S <suffix> Add custom suffix

In .bashrc:

 . <(type _known_hosts_real | sed -e '
 s/\(__lt\)/[ $notrim ]||\1/')

Now this works as expected:

$ _known_hosts_real -p "scp://" -S '/' -t -a gdn

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.