Use hostname completion in Bash

Jan 28, '04 10:45:00AM

Contributed by: patpro

Thanks to Kirk Bauer's tutorials in Automating UNIX and Linux administration, I was able to set up a small function for bash that provide user with hostname completion in the command line. Use your favorite editor to open your ~/.bashrc and paste this:


shopt -s progcomp
_hostname()
{
  local cur
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  # COMPREPLY is a 'one-liner'
  COMPREPLY=($(compgen -W '$( nigrep "name.*" / /machines | cut -d"
    " -f 5 | egrep -v "localhost|broadcasthost|machines")' -- $cur))
  # end of 'one-liner'
  return 0
}
# feel free to add commands after "ssh ping"
complete -F _hostname -o dirnames ssh ping
This code allows you to use hostname completion from NetInfo (bash has built-in hostname completion from /etc/hosts, but that's not really useful on Mac OS X).

Comments (7)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040120190250775