LINFO

How to Make Aliases Permanent



Aliases are a convenient feature of shells in Unix-like operating systems that make it possible to launch any command or group of commands (inclusive of any options, arguments and redirection) by merely entering a brief, pre-set string (i.e., sequence of characters).

Although aliases can easily be set up using the alias command, this command has the disadvantage that any alias created with it remains in effect only during the current login session (i.e., until the user logs out or the computer is shut down). This might not be much of a problem for systems that are rebooted (i.e., restarted) only infrequently (such as corporate database servers), but it can be a nuisance for systems that are frequently rebooted (e.g., home computers).

Fortunately, however, any alias can be made more enduring (i.e., until it is explicitly removed) by writing it to the appropriate configuration file with a text editor. The name and location of such file can vary according to the system and the shell. In the case of the bash shell (which is the default shell on Linux) on Red Hat Linux, an alias for any user can be added to the .bashrc file in that user's home directory.

Each alias is written in this file exactly the same as it would be written on the command line using the alias command (see The alias Command for examples). Because this configuration file is read at login, any changes to it will not take effect until the user has logged in again or opened a new terminal window (i.e., an all-text window in a GUI).

.bashrc is a hidden file, that is, a file whose name begins with a period and thus is not normally visible in a GUI (graphical user interface). However, it is easy to locate and open from the command line.

Because .bashrc is a plain_text file, it can easily be edited using any text editor, such as vi or gedit. For example, assuming that the user is currently in its home directory, the following could be used to open .bashrc with vi:

vi .bashrc

Likewise, the following could be used to open the same file with gedit:

gedit .bashrc

The alias should be typed in below the line that says # User specific aliases and functions. The pound sign at the start of this line indicates that the line is a comment, i.e., documentation for the convenience of humans rather than something on which the system acts.

Aliases for the root user (i.e., administrative account) can be made permanent by entering them in the .bashrc file in the root user's home directory (which is /root), i.e., in /root/.bashrc. System-wide aliases can be put in the /etc/bashrc file. (Note that, at least on some systems, /etc/bashrc is not a hidden file.) The system needs to be restarted before system-wide aliases can take effect.

If the unalias command, which is used to remove or suppress aliases, is used on an alias which has been recorded in a .bashrc file, that alias is turned off for the duration of the current login session. However, the alias is not removed from the .bashrc file, and when the user logs in again, that alias is again in effect.






Created August 6, 2005.
Copyright © 2005 The Linux Information Project. All Rights Reserved.