What is an alias? It isn’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 to pass options and arguments to the command. In principle, aliases can be anything you want. They are special names that you define to accomplish tasks. They aren’t shell scripts, as a shell script is external to your shell. To start up a shell script, type in its name. The system then starts a shell as a child process of your current shell to run the script.
Like commands, aliases are started by typing them in. However, they are internal to the shell (provided your shell supports aliases). That is, they are internal to your shell process. Instead of starting a sub-shell, the shell executes the alias internally. This has the obvious advantage of being quicker, as there is no overhead of starting the new shell or searching the hard disk.
Another major advantage is the ability to create new commands. You can do this with shell scripts (which we will get into later), but the overhead of creating a new process does not make it worthwhile for simple tasks. Aliases can be created with multiple commands strung together. For example, I created an alias, t, that shows me the time. Although the date command does that, all I want to see is the time. So, I created an alias t, like this:
When I type in t, I get the hours and minutes, just exactly the way I want. Granted one could create a shell script for this, but that would waste system resources as a new shell would need to get started each time.
Aliases can be defined in system-wide shell configuration file (e.g. /etc/profile) and are thus available for all users. Or a user can defined his or her own by defining them in either the .profile, .login or the .cshrc, depending on your shell. However, as I described above, if you want them for all sub-shells, they need to go in .cshrc. If you are running a Bourne Shell, aliasing may be the first good reason to switch to another shell.
Be careful when creating aliases or functions so that you don’t redefine existing commands. Either you end up forgetting the alias, or some other program uses the original program and fails because the alias gets called first. When I was in tech support, I once had a call from a customer with a system in which he could no longer install software. We tried replacing several programs on his system, but to no avail. Fortunately, he had another copy of the same product, but it, too, died with the same error. It didn’t seem likely that it was bad media. At this point, I had been with him for almost an hour, so I decided to hand it off to someone else (often, a fresh perspective is all that is needed).
About an hour later, one of the other engineers came into my cubicle with the same problem. He couldn’t come up with anything either, which relieved me, so he decided that he needed to research the issue. Well, he found the exact same message in the source code and it turned out that this message appeared when a command could not run the sort command. Ah, a corrupt sort binary. Nope! Not that easy. What else was there? As it turned out, the customer had created an alias called sort that he used to sort directories in a particular fashion. Because the particular command used to install the software package couldn’t work with his version of sort, it died.
When should you use an alias, function or shell script? Well, if there is something that can be done with a short shell script, then it can usually be done with a function. However, there are things that are difficult to do with an alias. One thing is making long, relatively complicated commands. Although you can often do this with an alias, it is much simpler and easier to read if you do it with a function. I will go into some more detail about shell functions later in the section on shell scripting. You can also find more details in the bash man-page.
On some systems, you will find that they have already provide a number of aliases for you. To see what aliases are currently configured, just run alias with no options and you might get something like this:
As you can see there are many different ways you can use aliases.
Sometimes you may need to remove an alias. For example, when connecting across a network to a remote machine where the ls alias is defined as above. In many cases $LS_OPTIONS includes color encoding for differ types of files. This often makes the output difficult to read. So, to remove the command you would issue the command: