Servers Host Completion in bash

Host Completion in bash

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.

Latest Posts

Compare HP’s iLo & Dell’s iDRAC Server Management Tools

Most servers shipped from the major manufacturers today come with some type of out-of-band management tool or baseboard management controller (BMC). Two of the...

Get-MsolUser PowerShell Attributes & Properties

This article has been updated for 2020. Please note that WAAD was retired in 2018, but the cmdlets listed in this article are still...

Microsoft Azure PowerShell Scripts and Commands

Using PowerShell scripts and commands for quickly executing tasks in Windows operating systems offers a number of benefits over traditional scripting languages, such as...

Microsoft Hyper V Review

Microsoft Hyper-V: The Bottom line Microsoft Hyper-V lagged behind VMware's virtualization tool, one of the most popular tools in the space, when it was first...

Best Cloud Based Services & Companies

Any company that’s delayed introducing cloud-based software into their infrastructure needs to consider leveraging these new technologies to reap all the benefits cloud computing...

Related Stories