{"id":192,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:25:16","modified_gmt":"2020-08-22T20:25:16","slug":"this-is-the-page-title-toplevel-26","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=192","title":{"rendered":"Aliases"},"content":{"rendered":"\n<title>Aliases<\/title>\n<p>\nWhat is an alias? It isn&#8217;t the ability to call yourself Thaddeus Jones when\nyour real name is Jedediah Curry. Instead, in a Linux-context it is the\nability to use a different name for a command, including the ability to pass\noptions and arguments to the command. In principle, aliases can be anything you want.\nThey are special names that you define to accomplish tasks. They aren&#8217;t\nshell scripts, as a shell script is external to your <glossary>shell<\/glossary>.\nTo start up a  shell script, type in its name. The system then starts a <glossary>shell<\/glossary>\nas a child process of your current <glossary>shell<\/glossary> to run the script.\n<\/p>\n<question id=\"58\" text=\"Aliases are internal to your shell.\" \/>\n<question id=\"59\" text=\"An alias is another name for the LOGNAME variable.\" \/>\n<concept id=\"98\" description=\"Aliases are internal to your shell.\" \/>\n<p>\nLike commands, aliases are started by typing them in. However, they are internal to the\n<glossary>shell<\/glossary> (provided your <glossary>shell<\/glossary>\nsupports aliases). That is, they are internal to your shell process.\nInstead of starting a <glossary>sub-shell<\/glossary>, the <glossary>shell<\/glossary>\nexecutes the <glossary>alias<\/glossary> internally. This\nhas the obvious advantage of being quicker, as there is no overhead of starting\nthe new <glossary>shell<\/glossary> or searching the hard disk.\n<\/p>\n<p>\nAnother major advantage is the ability to create new commands. You\ncan do this with shell scripts (which we will get into later), but the overhead\nof creating a new process does not make it worthwhile for simple tasks. Aliases\ncan be created with multiple commands strung together. For example, I created an\nalias, t, that shows me the time. Although the date command does that, all I\nwant to see is the time. So, I created an <glossary>alias<\/glossary> <i>t<\/i>, like this:\n<\/p>\n<p>\n<commandexample command=\"alias\">alias t=`date | cut -c12-16`<\/commandexample>\n<\/p>\n<concept id=\"\" description=\"An advantage of shell aliases is that they save system resources.\" \/>\n<question id=\"\" type=\"tf\" text=\"An advantage of shell aliases is that they save system resources.\" \/>\n<p>\nWhen I type in <i>t<\/i>, I get the hours and minutes, just exactly the way I want. Granted\none could create a shell script for this, but that would waste system resources as\na new shell would need to get started each time.\n<\/p>\n<p>\nAliases can be defined in system-wide shell configuration file (e.g. <file>\/etc\/profile<file>)\nand are thus available for all users. Or a user can defined his or her own by\ndefining them in either the <file>.profile<\/file>, <file>.login<\/file> or\nthe <file>.cshrc<\/file>, depending on your shell.\nHowever, as I described above, if you want them for all sub-shells,\nthey need to go in <file type=\"\">.cshrc<\/file>. If you are running a Bourne Shell, aliasing may be\nthe first good reason to switch to another <glossary>shell<\/glossary>.\n<\/p>\n<question id=\"60\" text=\"Since aliases are evaluated first, they are executed instead of commands of the same name.\" \/>\n<concept id=\"55\" description=\"Aliases will be executed before a command with the same name.\" \/>\n<question id=\"\" type=\"TF\" text=\"When parsing commands, the last thing that is evaluated are aliases. \" \/>\n<p>\nBe careful when\ncreating aliases or functions so that you don&#8217;t redefine existing commands.\nEither you end up forgetting the <glossary>alias<\/glossary>, or some other program uses the original\nprogram and fails because the <glossary>alias<\/glossary> gets called first. When I was in tech support,\nI once had a call from a customer with a system in which he could no longer install software. We tried\nreplacing several programs on his system, but to no avail. Fortunately, he had\nanother copy of the same product, but it, too, died with the same error. It\ndidn&#8217;t seem likely that it was bad media. At this point, I had been with him for\nalmost an hour, so I decided to hand it off to someone else (often, a fresh\nperspective is all that is needed).\n<\/p>\n<p>\nAbout an hour later, one of the other\nengineers came into my cubicle with the same problem. He couldn&#8217;t come up with\nanything either, which relieved me, so he decided that he needed to research the\nissue. Well, he found the exact same message in the source code and it turned\nout that this message appeared when a command could not run the <command>sort<\/command>\ncommand. Ah, a corrupt sort <glossary>binary<\/glossary>.\n Nope! Not that easy. What else was there? As it\nturned out, the customer had created an <glossary>alias<\/glossary>\ncalled sort that he used to sort\ndirectories in a particular fashion. Because the particular command used to\ninstall the software package couldn&#8217;t work with his version of sort, it died.\n<\/p>\n<p>\nWhen should you use an alias, function or shell script? Well, if\nthere is something that can be done with a short shell script, then it can usually be\ndone with a function. However, there are things that are difficult to do with an\nalias. One thing is making long, relatively complicated commands. Although you\ncan often do this with an <glossary>alias<\/glossary>,\n it is much simpler and easier to read if you do it\nwith a function. I will go into some more detail about <glossary>shell<\/glossary>\nfunctions later in the section on <glossary>shell<\/glossary>\nscripting. You can also find more details in the <command>bash<\/command> man-page.\n<\/p>\n<p>\nOn some systems, you will find that they have already provide a number of\naliases for you. To see what aliases are currently configured, just run\n<command>alias<\/command> with no options and you might get something\nlike this:\n<filecontents type=\"\">\nalias +=&#8217;pushd .&#8217;\nalias -=&#8217;popd&#8217;\nalias ..=&#8217;cd ..&#8217;\nalias &#8230;=&#8217;cd ..\/..&#8217;\nalias beep=&#8217;echo -en &#8220;\\007&#8243;&#8216;\nalias dir=&#8217;ls -l&#8217;\nalias l=&#8217;ls -alF&#8217;\nalias la=&#8217;ls -la&#8217;\nalias ll=&#8217;ls -l&#8217;\nalias ls=&#8217;ls $LS_OPTIONS&#8217;\nalias ls-l=&#8217;ls -l&#8217;\nalias md=&#8217;mkdir -p&#8217;\nalias o=&#8217;less&#8217;\nalias rd=&#8217;rmdir&#8217;\nalias rehash=&#8217;hash -r&#8217;\nalias unmount=&#8217;echo &#8220;Error: Try the command: umount&#8221; 1>&2; false&#8217;\nalias which=&#8217;type -p&#8217;\nalias you=&#8217;yast2 online_update&#8217;\n<\/filecontents>\n<p>\nAs you can see there are many different ways you can use aliases.\n<\/p>\n<p>\nSometimes you may need to remove an alias. For example, when connecting across a\nnetwork to a remote machine where the <i>ls<\/i> alias is defined as\nabove. In many cases $LS_OPTIONS includes color encoding for differ types of files.\nThis often makes the output difficult to read. So, to remove the command you would\nissue the command:\n<\/p>\n<p>\n<commandexample command=\"alias\">unalias ls<\/commandexample>\n<\/p>\n<question id=\"\" type=\"mc\" text=\"What command would creat an alias to move a file to $HOME\/trash, rather than deleting it.\" \/>\n","protected":false},"excerpt":{"rendered":"<p>Aliases What is an alias? It isn&#8217;t the ability to call yourself Thaddeus Jones when your real name is Jedediah Curry. Instead, in a Linux-context it is the ability to use a different name for a command, including the ability &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=192\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-192","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/192","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=192"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/192\/revisions"}],"predecessor-version":[{"id":498,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/192\/revisions\/498"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=192"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}