{"id":1045,"date":"2020-05-13T02:27:06","date_gmt":"2020-05-13T01:27:06","guid":{"rendered":"https:\/\/codedoneright.eu\/?page_id=1045"},"modified":"2020-07-24T01:52:46","modified_gmt":"2020-07-24T00:52:46","slug":"alias","status":"publish","type":"page","link":"https:\/\/codedoneright.eu\/?page_id=1045","title":{"rendered":"Shell alias"},"content":{"rendered":"\n<p>If you are using Bash then, most likely, you have some commands or a script that you run quite often, alias is the way to go about it. For me that would be a script called <em>cdr_backup.sh<\/em> that helps me make a backup of this server.<\/p>\n\n\n\n<p>Now, if you are running such a script with any frequency whatsoever it would be inconvenient to type the path to said script every time you need to run it. <\/p>\n\n\n\n<p>Let me give you an example \u2013 <em>date<\/em> is a command to print or set the system date and time, and the actual command points to <em>\/usr\/bin\/date<\/em>. To get the current date without an alias you would have to type <em>\/usr\/bin\/date<\/em> every time, but since there is a convenient alias in place you can just type date and the result is the same. <\/p>\n\n\n\n<p>Try the following three commands<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>which date\n\/usr\/bin\/date\ndate<\/code><\/pre>\n\n\n\n<ul><li>The first one shows us where the date command is located in the system<\/li><li>The second one is the absolute path to that command and executes it<\/li><li>Third one is an alias to the command<\/li><\/ul>\n\n\n\n<p>This is the power of an alias. Moreover, aliases can be used in script as well.<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Creating an alias<\/h4>\n\n\n\n<p>Creating an alias is a very simple process. Take a look at <em>ls<\/em> command. It lists contents of a directory. In your terminal type<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls ~<\/code><\/pre>\n\n\n\n<p>You should see the contents of your Home directory. Now type <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls -la ~<\/code><\/pre>\n\n\n\n<p>You will be presented with a more detailed list of files since <em>-la<\/em> arguments changes the behavior of <em>ls<\/em> command to show output as a list and include all files, even if they are hidden.<\/p>\n\n\n\n<p>I always use <em>-la<\/em> flags so instead of typing <em>ls -la &lt;directory&gt;<\/em> I created an alias so that every time I type <em>ls<\/em> it actually processes <em>ls -la<\/em> instead. In order to do that type <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias ls=\"ls -la\"<\/code><\/pre>\n\n\n\n<p>Now list your Home directory again by using <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ls ~<\/code><\/pre>\n\n\n\n<p>The result should be the same as <em>ls -la<\/em>, but you might have lost color hints. That is expected. Do not worry this alias is temporary.<\/p>\n\n\n\n<p>Take a look at the syntax of the following<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias $1=\"$2\"<\/code><\/pre>\n\n\n\n<ul><li>alias \u2013 this command tells the shell that you want to create an alias<\/li><li>$1 \u2013 denotes what alias you want to use, put a string of text here that you want to type in bash to get the result from $2<\/li><li>$2 \u2013 this is a command you are making alias of, here you can put anything that you want executed. A command with additional arguments, a path to a shell script etc.<\/li><\/ul>\n\n\n\n<p>If you want to make an alias that will show all files in your Home folder just by typing <em>show_home<\/em> you can do that with<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias show_home=\"ls -la ~\"<\/code><\/pre>\n\n\n\n<p>Now every time you type <em>show_home<\/em>, shell will execute <em>ls -la ~<\/em> It is not the most useful example but an easy one to understand.<\/p>\n\n\n\n<p>You can check aliases set on your system by simply typing <em>alias<\/em> in your terminal without any additional arguments. Try it to check what aliases you have that you might not know of, and at the same time you will confirm that <em>show_home<\/em> is recognized by your shell.<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Removing an alias<\/h4>\n\n\n\n<p>This one is even simpler, to remove the alias that you just created simply type<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>unalias show_home<\/code><\/pre>\n\n\n\n<p>Now if you type show_home in your terminal is should simply say<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>show_home: command not found<\/code><\/pre>\n\n\n\n<p>Your alias is gone. Confirm by typing <em>alias<\/em> in your terminal<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Temporary vs permanent alias<\/h4>\n\n\n\n<p>If you set an alias in your shell by using the <em>alias<\/em> command it is just a temporary alias. If you log out it is gone. Try it out, make an alias, relog and try using it again. It is not there anymore. <\/p>\n\n\n\n<p>If you want to make a permanent alias you have to include it in <em>~\/.bashrc<\/em> file. Each user has its own file and any changes will affect only that user. If you set up an alias on one account it will not work on the other.<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Creating a permanent alias<\/h4>\n\n\n\n<p>Now try adding <em>show_home<\/em> as a permanent alias, use the following command to modify your <em>.bashrc<\/em> file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>nano ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>Navigate to the end of the file and place the following inside<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias show_home=\"ls -la ~\"<\/code><\/pre>\n\n\n\n<p>Save and close the file and issue the command to re-read the contents of the file<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>source ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>Alternatively you can just relog, but sourcing is much more convenient.<\/p>\n\n\n\n<p>Now you have a permanent alias that will be available every time you log in.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Removing a permanent alias<\/h6>\n\n\n\n<p>As you probabily figured that out you can just remove the line with your alias from the <em>.bashrc<\/em> file, source it again and it is gone. I am making a note for the completeness of the tutorial.<\/p>\n\n\n\n<h6 class=\"wp-block-heading\">Another way of adding a permanent alias<\/h6>\n\n\n\n<p>You can also insert your alias by appending the <em>.bashrc<\/em> file from the command line like so<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"alias show_home='ls -la ~'\" >> ~\/.bashrc<\/code><\/pre>\n\n\n\n<p>The <em>echo<\/em> command redirects its standard output to append the <em>.bashrc<\/em> file. Note that I used single quotes for the command to be aliased, and double quotes for the full line that is supposed to be added.<\/p>\n\n\n\n<p><strong>Caution! <\/strong>Remember that <strong>&gt;&gt;<\/strong> will add to the file, however, <strong>&gt;<\/strong> will substitute the file contents. Read more about redirecting a command output for more information. In essence &gt; will destroy your file, &gt;&gt; will add to your file.<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">What else can you do with an alias?<\/h4>\n\n\n\n<p>In the first paragraph I mentioned <em>cdr_backup.sh<\/em> script that I run quite often. If I were to run it manually everytime I want to make a backup I would have to type<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/var\/zzz_script\/cdr_backup.sh<\/code><\/pre>\n\n\n\n<p>every time, however, I set up an alias so that I can just type <em>cdr_backup<\/em> in terminal and the script is executed. The entry in <em>.bashrc<\/em> file looks like so<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>alias cdr_backup='\/var\/zzz_script\/cdr_backup.sh'<\/code><\/pre>\n\n\n\n<p>You can check out the script <a href=\"http:\/\/codedoneright.eu\/?page_id=1074\" target=\"_blank\" aria-label=\"undefined (opens in a new tab)\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<div style=\"height:56px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Conclusion<\/h4>\n\n\n\n<p>Bash helps automating tasks and alias is just one more way to do so. It is also more convenient to use if you have a dedicated folder with script that you use with any frequency, or if you use certain commands with additional arguments all the time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are using Bash then, most likely, you have some commands or a script that you run quite often, alias is the way to go about it. For me that would be a script called cdr_backup.sh that helps me&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1026,"menu_order":40,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Shell alias &#8212; Code Done Right!<\/title>\n<meta name=\"description\" content=\"A quick look at alias command in linux. Learn how to create an alias an make it permanent, remove unnecessary aliases and list them\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codedoneright.eu\/?page_id=1045\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"Shell alias &#8212; Code Done Right!\" \/>\n<meta name=\"twitter:description\" content=\"A quick look at alias command in linux. Learn how to create an alias an make it permanent, remove unnecessary aliases and list them\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codedoneright.eu\/?page_id=1045\",\"url\":\"https:\/\/codedoneright.eu\/?page_id=1045\",\"name\":\"Shell alias &#8212; Code Done Right!\",\"isPartOf\":{\"@id\":\"https:\/\/codedoneright.eu\/#website\"},\"datePublished\":\"2020-05-13T01:27:06+00:00\",\"dateModified\":\"2020-07-24T00:52:46+00:00\",\"description\":\"A quick look at alias command in linux. Learn how to create an alias an make it permanent, remove unnecessary aliases and list them\",\"breadcrumb\":{\"@id\":\"https:\/\/codedoneright.eu\/?page_id=1045#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codedoneright.eu\/?page_id=1045\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codedoneright.eu\/?page_id=1045#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codedoneright.eu\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Linux\",\"item\":\"https:\/\/codedoneright.eu\/?page_id=1026\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Shell alias\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codedoneright.eu\/#website\",\"url\":\"https:\/\/codedoneright.eu\/\",\"name\":\"Code Done Right!\",\"description\":\"Raspberry Pi server guides\",\"publisher\":{\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codedoneright.eu\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568\",\"name\":\"CodeDoneRight\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png\",\"contentUrl\":\"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png\",\"width\":120,\"height\":120,\"caption\":\"CodeDoneRight\"},\"logo\":{\"@id\":\"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/codedoneright.eu\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Shell alias &#8212; Code Done Right!","description":"A quick look at alias command in linux. Learn how to create an alias an make it permanent, remove unnecessary aliases and list them","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/codedoneright.eu\/?page_id=1045","twitter_card":"summary_large_image","twitter_title":"Shell alias &#8212; Code Done Right!","twitter_description":"A quick look at alias command in linux. Learn how to create an alias an make it permanent, remove unnecessary aliases and list them","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codedoneright.eu\/?page_id=1045","url":"https:\/\/codedoneright.eu\/?page_id=1045","name":"Shell alias &#8212; Code Done Right!","isPartOf":{"@id":"https:\/\/codedoneright.eu\/#website"},"datePublished":"2020-05-13T01:27:06+00:00","dateModified":"2020-07-24T00:52:46+00:00","description":"A quick look at alias command in linux. Learn how to create an alias an make it permanent, remove unnecessary aliases and list them","breadcrumb":{"@id":"https:\/\/codedoneright.eu\/?page_id=1045#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codedoneright.eu\/?page_id=1045"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codedoneright.eu\/?page_id=1045#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codedoneright.eu\/"},{"@type":"ListItem","position":2,"name":"Linux","item":"https:\/\/codedoneright.eu\/?page_id=1026"},{"@type":"ListItem","position":3,"name":"Shell alias"}]},{"@type":"WebSite","@id":"https:\/\/codedoneright.eu\/#website","url":"https:\/\/codedoneright.eu\/","name":"Code Done Right!","description":"Raspberry Pi server guides","publisher":{"@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codedoneright.eu\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/50378701e349dbd5d40888bc5b532568","name":"CodeDoneRight","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/","url":"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png","contentUrl":"https:\/\/codedoneright.eu\/wp-content\/uploads\/www_icon.png","width":120,"height":120,"caption":"CodeDoneRight"},"logo":{"@id":"https:\/\/codedoneright.eu\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/codedoneright.eu"]}]}},"_links":{"self":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1045"}],"collection":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1045"}],"version-history":[{"count":12,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1045\/revisions"}],"predecessor-version":[{"id":1194,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1045\/revisions\/1194"}],"up":[{"embeddable":true,"href":"https:\/\/codedoneright.eu\/index.php?rest_route=\/wp\/v2\/pages\/1026"}],"wp:attachment":[{"href":"https:\/\/codedoneright.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1045"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}