{"id":248,"date":"2009-05-04T01:52:38","date_gmt":"2009-05-04T06:52:38","guid":{"rendered":"http:\/\/yourlinuxguy.com\/?p=248"},"modified":"2009-05-04T09:17:01","modified_gmt":"2009-05-04T14:17:01","slug":"cool-bash-completion-trick","status":"publish","type":"post","link":"https:\/\/yourLinuxGuy.com\/?p=248","title":{"rendered":"Cool bash completion trick&#8230;"},"content":{"rendered":"<p>Here&#8217;s a cool trick for you folks&#8230;<\/p>\n<p>If you regularly <code>ssh<\/code> to a set of servers in your server farm, but don&#8217;t like having to type out the full hostname on the command line (or can&#8217;t remember all the ip addresses), I&#8217;ve got a tip for you.<\/p>\n<p>I&#8217;m sure you already know that you can type a part of a filename on the command line, and have <code>bash<\/code> complete the name.\u00a0 For instance, you would type:<\/p>\n<p><code>vi \/etc\/hos[tab]<\/code><\/p>\n<p>&#8230;and the tab key completes the word &#8220;<code>host<\/code>&#8221; (pressing tab twice shows the remaining matching options that start with <code>\/etc\/hosts<\/code>, including <code>\/etc\/hosts<\/code>).\u00a0 But did you know that <code>bash<\/code> completion can work for more things that just the filesystem?\u00a0 Well, you do now.<\/p>\n<p>So now let&#8217;s apply this capability to <code>ssh<\/code>, since this is something many of us commonly use.\u00a0 Normally if you are trying to <code>ssh<\/code> to a server, you probably type something like this:<\/p>\n<p><code>ssh longservernamethatyoudontwanttotype<\/code><\/p>\n<p>So, if you happen to have all your servers\u00a0 in your local <code>\/etc\/hosts<\/code> file, we can add them to the <code>bash<\/code> shell&#8217;s tab completion capability with the following command:<\/p>\n<p><code>complete -W \"`awk '{ print $2 }' \/etc\/hosts`\" ssh<\/code><\/p>\n<p>&#8230;and be very careful there to note the back-ticks and double-quotes.\u00a0 In case you are wondering, what this is doing is to take the second column of your hosts file and put them into a list for completion of any <code>ssh<\/code> command.\u00a0 Of course, you could add this to a startup script or some such, but I&#8217;m not going into that here for the sake of brevity.\u00a0 Anyway, now if you type:<\/p>\n<p><code>ssh long[tab]<\/code><\/p>\n<p>&#8230;then the rest of the long servername will be completed there on the line.\u00a0 Is that cool or what?<\/p>\n<p>Alternately, if you happen to have the servernames listed in a textfile, or if you need to prefix your <code>ssh<\/code> connections with a username, you could create that server list file like this (with or without the username prefix, depending on how you wish to connect):<\/p>\n<p><code>jpavlov@abrams<br \/>\njpavlov@backus<br \/>\njpavlov@crowell<\/code><\/p>\n<p>&#8230;and so on.\u00a0 This way, the tab completion will include your username prefix. And to utilize this particular file, you would enter something like this (of course, pointing to your particular server list, not mine):<\/p>\n<p><code>complete -W \"`cat \/home\/jpavlov\/serverlist.txt`\" ssh<\/code><\/p>\n<p>&#8230;and again, be careful with the quotes and back-ticks.<\/p>\n<p>I hope this makes your life easier folks, it sure has helped me&#8230; And if you haven&#8217;t figured it out already, this process applies to just about any program you want.\u00a0 And, you can actually use more precise functions instead of the command method I describe here, but that&#8217;s a lot more complex than I wanted to get into in this post.\u00a0 Make sure to check out the already-existing function files at <code>\/etc\/bash_completion<\/code> and check out <code>complete --help<\/code> for more details&#8230;<\/p>\n<p>Enjoy!<\/p>\n<p>\ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a cool trick for you folks&#8230; If you regularly ssh to a set of servers in your server farm, but don&#8217;t like having to type out the full hostname on the command line (or can&#8217;t remember all the ip&#8230;<br \/><a class=\"read-more-button\" href=\"https:\/\/yourLinuxGuy.com\/?p=248\">Read more<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[67,10],"tags":[],"class_list":["post-248","post","type-post","status-publish","format-standard","hentry","category-bash","category-introductory"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/pnjn1-40","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/248","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=248"}],"version-history":[{"count":5,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions"}],"predecessor-version":[{"id":250,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=\/wp\/v2\/posts\/248\/revisions\/250"}],"wp:attachment":[{"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=248"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=248"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/yourLinuxGuy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=248"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}