#124: Exclude /etc/hosts from bash_completion

I just installed the homebrew package bash_completion, hoping to get completion on my SSH commands. Unfortunately, I also use my /etc/hosts file as an ad blocker. This results in bash completion completing my ssh hostnames with a ton of ad servers that I put into /etc/hosts, instead of just the few in ~/.ssh/known_hosts that I actually use.

I need a way to exclude these /etc/hosts matches from bash_completion for SSH.


After digging through the code, it seems that setting the environment variable COMP_KNOWN_HOSTS_WITH_HOSTFILE to an empty value will exclude /etc/hosts from bash completion on SSH. This leaves just the known_hosts file as the source for completions.

On homebrew, for example, adding the line COMP_KNOWN_HOSTS_WITH_HOSTFILE="" before the bash source include

if [ -f $(brew --prefix)/etc/bash_completion ]; then COMP_KNOWN_HOSTS_WITH_HOSTFILE='' # this suppresses use of /etc/hosts . $(brew --prefix)/etc/bash_completion fi

will exclude the misc. matches from completion.


  1. The same result can be accomplished by adding


    to your .bashrc, rather than modifying the bash_completion source.

    sean_the_bean on June 20, 2016, 07:52 PM UTC

Think you've got a better solution? Help 92049143cabb7ba896d7c06e19906303_small yliu out by posting your solution