Bash hostname completion | Arabesque
As part of its programmable completion suite, Bash includes hostname completion. This completion mode reads hostnames from a file in hosts(5)
format to find possible completions matching the current word. On Unix-like operating systems, it defaults to reading the file in its usual path at /etc/hosts
.
For example, given the following hosts(5)
file in place at /etc/hosts
:
127.0.0.1 localhost 192.0.2.1 web.example.com www 198.51.100.10 mail.example.com mx 203.0.113.52 radius.example.com rad
An appropriate call to compgen
would yield this output:
$ compgen -A hostname localhost web.example.com www mail.example.com mx radius.example.com rad
We could then use this to complete hostnames for network diagnostic tools like ping(8)
:
$ complete -A hostname ping
Typing ping we
and then pressing Tab would then complete to ping web.example.com
. If the shopt
option hostcomplete
is on, which it is by default, Bash will also attempt host completion if completing any word with an @
character in it. This can be useful for email address completion or for SSH username@hostname
completion.
Read full article from Bash hostname completion | Arabesque
No comments:
Post a Comment