SHARE
Facebook X Pinterest WhatsApp

Host Completion in bash

Written By
thumbnail Juliet Kemp
Juliet Kemp
Jul 20, 2010
ServerWatch content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More



Juliet Kemp

Hitting tab to complete commands and filenames is great. Extending this to hostnames is even better. (Does it show that I’m very lazy?) One way of doing this is to switch to the zsh shell, which does this and a bunch of other stuff by default. Happily, though, if you’d rather stick with
bash, all is not lost.

Tip of the Trade: One way to get the most out of bash’s programmable parameter completion is to set up hostname completion. Here’s how to code it yourself.

bash has programmable parameter completion, so you could set
hostname completion up by coding it yourself. That’s not necessary, though. Instead, you can pick up the bash completion files (also available as the bash-completion package in RH, Debian and from Darwin Ports for Mac OS X).

Once you’ve installed that, type:

. /etc/bash_completion	

(path on a Mac is /opt/local/etc/bash_completion). Add this to your
~/.bashrc or ~/.bash_profile to load it automatically on
startup. Now, try typing ssh and a couple of letters of a machine
you often ssh to, then hit Tab. Bingo! You’ll get either a list of options,
or, if there’s only one option available, the name filled in, just like with
the tab-completion you’re already used to.

This takes information from your .ssh/known_hosts file. It works
automatically for ssh and similar commands, but you can also set other
commands to use it. For example, maybe you have a script called ‘update’
which updates a particular host. Type:

complete -F _known_hosts update

and you should now be able to tab-complete hosts with that, as well. (If you
have problems, try specifying its full path.)

To investigate programmable completion options more, complete -p
will list all the currently available completions.

thumbnail Juliet Kemp

Juliet Kemp is a ServerWatch contributor.

Recommended for you...

What Is a Container? Understanding Containerization
What Is a Print Server? | How It Works and What It Does
Nisar Ahmad
Dec 8, 2023
What Is a Network Policy Server (NPS)? | Essential Guide
Virtual Servers vs. Physical Servers: Comparison and Use Cases
Ray Fernandez
Nov 14, 2023
ServerWatch Logo

ServerWatch is a top resource on servers. Explore the latest news, reviews and guides for server administrators now.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.