{"id":1800,"date":"2014-01-03T13:08:43","date_gmt":"2014-01-03T21:08:43","guid":{"rendered":"http:\/\/www.wellgolly.com\/?p=1800"},"modified":"2014-02-02T08:32:52","modified_gmt":"2014-02-02T16:32:52","slug":"bash-using-an-alias-in-an-alias","status":"publish","type":"post","link":"https:\/\/www.wellgolly.com\/?p=1800","title":{"rendered":"Bash: Using an alias in an alias"},"content":{"rendered":"<p>I\u2019ve been helping set up some VPSs (Virtual Private Server) and at the moment they don\u2019t have domain names assigned. I can use the history command to find the right IP address to ssh in but that can be cumbersome. So I decided to write an alias for each server. These lines are in my extended bash config file that is called when my .profile runs.<\/p>\n<p>My login is the same on each server so I could hard code this, but I made it general so I can share it with other users. I don\u2019t like to hard code things so I thought I\u2019d let bash find my user id and then use it in the ssh command. My first thought was to just create an alias for my login and use it in an alias for each server. But I couldn\u2019t get it to work. I tried using just the alias since examples of creating aliases seem to work this way with builtin commands. Then I thought that maybe I need to escape the alias so that bash knows that it should process it.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nalias myID=&#039;who am i | cut -d\\&nbsp;&nbsp;-f 1&#039;\nalias myserver=&#039;ssh myID@192.245.184.221&#039;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#Doesn\u2019t work\nalias myserver=&#039;ssh $(myID)@192.245.184.221&#039;&nbsp;&nbsp; #Works fine\n<\/code><\/pre><\/p>\n<p>It turns out that the second line works, but I did had a problem with aliases being set and it didn\u2019t work for me. So I started playing around with things that did work and making small adjustments. FYI, One thing you need to do if you redefine aliases and functions is to make sure that you clear out the old ones. If you don\u2019t then you may not be running the function you thought you were or you could be running an alias when you thought you were running a function. <\/p>\n<p>You can use unset to remove them from memory. Use the -f flag for functions.<br \/>\n<pre><code class=\"preserve-code-formatting\">\nunset myID\nunset -f dave\n<\/code><\/pre><\/p>\n<p>For myserver, I put the alias command in parens and prefixed it with a $. Bash executes this first and then uses it in the ssh command. If you don\u2019t do this, bash thinks that the first argument for ssh is &#8216;who&#8217; and fails. That worked, but I don\u2019t like the idea of having duplicate code in login alias. So I thought I\u2019d try a function. In the first function, I define a variable and then use it. The third function looks an awful lot like a failed alias in the example above, but since it is a function, it works. I think what is happening is that when bash sees $(myID) it thinks, I should process whatever is inside the parens. So it finds the alias and runs it.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n#&nbsp;&nbsp; Log in to other systems\nalias myID=&#039;who am i | cut -d\\&nbsp;&nbsp;-f 1&#039;\n\nalias myserver=&#039;ssh $(who am i | cut -d\\&nbsp;&nbsp;-f 1)@192.245.184.221&#039;\ndave() { id=$(who am i | cut -d\\&nbsp;&nbsp;-f 1); ssh $id@192.233.221.11; }\ndana()&nbsp;&nbsp;{ ssh $(myID)@192.149.142.163 ; }\n\nalias bigserver=&#039;ssh &quot;echo $(myID)&quot;@192.255.174.220&#039;\n<\/code><\/pre><\/p>\n<p>It finally occurred to me that the .profile script is a full blown shell script. So that means I can use variables in it. I define an id variable that is the result of the shell executing the commands that are in it. When the .profile is run, bash assigns id to my login. Then I can use it anywhere I want with $id. I don\u2019t need to use ${id} since my login is one word and since the @ cannot be in a variable name, bash knows to stop processing when it hits the @. This is what I wanted to do with aliases. I thought about making it a local variable since I don\u2019t need it to hang around outside of this script. But the alias isn\u2019t resolved until it is used, so it needs to resolve the variable when it is called.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n#&nbsp;&nbsp; Log in to other systems\nid=$(who am i | cut -d\\&nbsp;&nbsp;-f 1)\nalias myserver=&#039;ssh $id@192.245.184.221&#039;\nalias dave=&#039;ssh $id@192.233.221.11&#039;\nalias dana=&#039;ssh $id@192.149.142.163&#039;\n<\/code><\/pre><\/p>\n<p>I started playing around with aliases again and got strictly aliases to work as well.<br \/>\n<pre><code class=\"preserve-code-formatting\">\n#&nbsp;&nbsp; Log in to other systems\nalias myID=&#039;who am i | cut -d\\&nbsp;&nbsp;-f 1&#039;\nalias myserver=&#039;ssh $(myID)@192.245.184.221&#039;\nalias dave=&#039;ssh $(myID)@192.233.221.11&#039;\nalias dana=&#039;ssh $(myID)@192.149.142.163&#039;\n<\/code><\/pre><\/p>\n<p>Update: I was playing with <i>printenv<\/i> and it turns out that one of the environment variables is LOGNAME. So you can replace the $id with $LOGNAME in the code above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I\u2019ve been helping set up some VPSs (Virtual Private Server) and at the moment they don\u2019t have domain names assigned. I can use the history command to find the right IP address to ssh in but that can be cumbersome. So I decided to write an alias for each server. These lines are in my &hellip; <a href=\"https:\/\/www.wellgolly.com\/?p=1800\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Bash: Using an alias in an alias<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1800","post","type-post","status-publish","format-standard","hentry","category-coding"],"_links":{"self":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1800","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1800"}],"version-history":[{"count":0,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=\/wp\/v2\/posts\/1800\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1800"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1800"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wellgolly.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}