Tuesday, July 11, 2006

 

The Lovely alias Command


alias
allows you to

a. Give a command a new name, or


b. Assign a command some desirable behavior by default.


The syntax is
alias new_name='old name'
or

alias command='command -switch'

In other words, the alias (the new name for a command or the name you wish to use for the new behavior of a command) always follows 'alias'. To the right of the equals sign, between single quotation marks, figure either the old name of the command or the new behavior assigned.

Warning: by default, alias is only valid for the current session. All changes will be lost upon logging out. To make aliases permanent, it is necessary to write them into .bashrc (see post).

The renaming can be handy if, for example, you'd like to give commands a name that you relate to more than the default name:

alias delete='rm'

will give the rm (remove) command a new name (alias), namely 'delete'.


As a result, it will be possible to carry out the operation of removing files with either delete or rm.
alias, thus, gives the command an additional name - it does not replace the previous name.


The second way of using alias, to change the default mode of a command, can be handy if you wish a particular switch to be the default mode of a command (to save typing, or, more importantly, since you might forget to type it altogether): as a result of

alias rm='rm -i',
for example, doing rm will actually mean doing rm -i. This happens to be important, as the -i switch means that the machine will ask you whether you're sure that you want to remove the file concerned. Thanks to the alias, the machine will ask for confirmation even when you do rm absent-mindedly... Likewise, any command can be made to carry out by default any behaviour you happen to prefer.

Of course, the two uses of alias can be combined - the new default behavior can also be given a new name if you like:


alias delete='rm -i'.



(To be aware of: The system will allow you to assign a command a dedicated word already used as a command name. In that case, the dedicated word will denote the alias, not the command it usually denotes. Thus, as a result of
alias mv='ls'
mv will behave as an ls command, and not as the usual mv (move)... This behavior depends on the PATH variable, as fully stating the address of mv returns the usual mv behavior:
/bin/mv pilot pilot1

simply renames the file 'pilot'.
In short, it's best to avoid dedicated words already used by the system. When in doubt, a quick man or apropos on the
new candidate name will tell if it's a dedicated word.)

To discard an alias, unalias will do:

unalias delete
means that 'delete' will no longer be a command.

To list all current aliases, just do 'alias'.

You can't do man alias, as alias, being a built-in shell command, is described in man bash. Further details and formal description of alias on: http://www.computerhope.com/unix/ualias.htm


alias is just one example, I guess, of what is meant by the power and flexibility of Linux.


Comments: Post a Comment

Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?