Linux+: Linux Shell 20 – Alias Command


Jarret W. Buse

Linux+: Linux Shell 20 – Alias Command

Using the shell's command-line can be cumbersome to some people. Unfortunately, it may be necessary at times to use the shell. Since some people use it very little, they cannot remember all the commands. Some spend more time trying to recall or search for a specific command name than it takes to run the command and get the desired result.
Even if an alias is used, the options and syntax are the same. The only change with an alias is that the command name itself will be different.

For example, for someone used to DOS, they could use “type” instead of “cat” to display files or “cls” instead of “clear”.

The syntax for the alias command is:

alias new_command=shell_command

To use the previous examples of DOS commands, you could use the following for the “type” command:

alias type=cat

The other example with “cls” would be:

alias cls=clear

If you wish to list the commands for which you have created an alias, you would simply type “alias” at the shell prompt. What do you do if you have a long list of aliases and you need to see if you have an alias for a specific command? For example, if I want to check what my alias is for “cls”, if I even have one, I would type the following:

alias cls

If the alias “cls” does not exist, then I would receive an error since the alias was not found. If the alias does exist, it will list the given alias, such as:

alias cls='clear'

If an alias exists, you can simply overwrite the value by performing the alias again and using a different “shell_command”. For instance, if the command “alias cls=claer” were typed into a shell, it would do nothing. In this case the command “claer” is spelled wrong. It can be fixed by retyping the command correctly as “alias cls=clear”.

An alias can also include parameters and other commands. For example, to open an editor like gedit with root privileges, do this command:

alias sedit='sudo gedit'

When you type “sedit” followed by any filenames, the command “sedit” is replaced with “sudo gedit” and the filenames are placed after the alias command. If you should want to use “type” instead of the “cat” command and also have the lines numbered with the “-n” option, the alias command would be:
alias type='cat -n'

NOTE: If a space will be in the alias, you need to enclose the whole command in single quotes (' ').
If you want the file typed with the “more” command, you can do the following:

alias type=more

When an alias is created, it is temporary. Once the shell is closed, the aliases are lost. Aliases can be made permanent by entering them into the /etc/bash.bashrc file.

A shortcut to automate the process is to include an external file of aliases to the /etc/bash.bashrc file. Within the bash.bashrc file, you can include another file after the alias command “alias ll='ls -al'”. The statements to include a file are:

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases

You then need to create a file named “bash_aliases” in your home folder. An easy way to copy your current aliases to the file is to an alias command such as the following:
alias save='alias > ~/.bash_aliases'

At a shell prompt, type save and press enter. All current aliases will be written to the file specified (~/.bash_aliases). The trick is a useful way to make your aliases permanent. At any time, you can go into the “~/.bash_aliases” file and remove any line you no longer need.

As lines are aliased in the shell, these are short-term aliases. When they are saved to the “.bash_aliases” file, they are then considered long-term. If the “.bash_aliases” file is edited manually, the changes are not effective until the user logs out and back in.

Another way to remove an alias is the unalias command. Once used, the alias is removed and you can then use the new “save” command to update the alias list. The unalias command's syntax is:

unalias alias_name

There is one option; the '-a' can be used to remove all aliases in the alias list.
If you create an alias called “cls” it can be removed by typing:

unalias cls

If you set up the process to save the alias list, you must save the list after you unalias an item. If you do not save the list, the item will be reloaded when the terminal starts again.

Unalias does allow for the removal of multiple aliases without removing all of them. For example, to remove the alias “type” and “cls” we would use the following command:

unalias type cls

Separate multiple aliases with a single space for multiple aliases to be removed at once.

Keep in mind that the alias command can be a very useful tool. One problem which may arise is that you become used to your aliases and forget the original commands. When you are in front of a system without your aliases, you may struggle to find the correct commands.

For some people, it may be best to store their “.bash_aliases” file on a network server so it can be accessed anywhere. If needed, keep it on a thumb drive to keep with you so you can always use it, ot at least look at the file to see what the real command name is that you need.


  • slide.jpg
    49.4 KB · Views: 9,876