Translate

Wednesday, December 26, 2012

Alias command in Unix/Linux

The alias command makes it possible to launch any command or group of commands (inclusive of any options,arguments and redirection) by entering a pre-set string.For instance,a user might want to navigate to a directory /var/log/apache2.In a normal scenario he will issue the command cd /var/log/apache2.However,it is possible to navigate to the same directory using a single word/string.This is possible with the alias command.

The alias command allows a user to create simple names or abbreviations(even a single character is allowed) for commands regardless of how complex the original commands are and then use them in the same way as normal commands are used.

The alias command is built into a number of shells including ash,bash,ksh and csh.It is one of the ways to customize the shell.Aliases are recognized only by the shell in which they are created and are applicable to the user who creates them unless that user is 'root'.

 

Listing and creating aliases 

The general syntax for the alias command is
alias [-p] [name='value']

name is the name of the new alias and value is the command(s) which it initiates.The alias name and value can be any valid shell input except the equals(=) sign.The option -p displays a list of the aliases for the current user on the current shell.

When used with no arguments and with/without the -p option,alias provides a list of aliases that are in effect for the current user.
alias













To demonstrate the above described example we create an alias 'apachelogs' which when used will set the current directory to /var/log/apache2.



 Multiple commands can be included in the same alias by inserting them within the same quotation marks and seperating them with a semi-colon



In the above example,we create an alias sid which will execute the pwd command followed by the ls command.

Not only can options and arguments be used in the command(s) that an alias can substitute for, but they can also be used with an alias that has already been created. As a trivial example, supposing the alias l is created for the ls command:
 alias l='ls -a'

Then, the alias l could be used with any argument that the command ls could be used with. For example, to list the files and directories in the the /var directory:
 l /var  

 

Removing aliases

The command unalias is used to remove previously created aliases.Its syntax is
unalias [-a] [name(s)]

For example, the following would remove the alias apachelogs which was created earlier
unalias apachelogs














unalias removes not only aliases created during the current session but also permanent aliases that are listed in system configuration files. The -a option tells unalias to remove all aliases for the current user for the current shell.

 

Making aliases permanent

The aliases we created above remain in effect only during the current login session (i.e., until the user logs out or the computer is shut down).To make aliases permanent we can write them in a configuration file named .bashrc that is located in the user's home directory.This file is read every time by the shell at login.Aliases for the root user can be made permanent by entering them in the .bashrc file in the root's home directory, i.e., in /root/.bashrc

We can edit the .bashrc file using a simple text editor such as vi.Execute the following command in the home directory
vi .bashrc

Here we can add any alias we want.After adding them save the file and exit the editor.For the aliases to come in effect either reboot the system or execute the .bashrc file by executing this command from the terminal
           . ~/.bashrc

Remember to give a space between the .(dot) and the ~(tilde). This will make the aliases permanent.

1 comment:

sridhar k said...

Thanks.!! it ll be useful..