Setting aliases with alias

Aliases are nicknames of sorts that you use to enter commands more easily. For example, if you frequently use the command mail -s "Lunch today? deb .signature, you could set an alias for this command and call it lunch. Then, in the future, all you have to do is type in lunch, and the result is the same as if you typed in the longer command (Figure 8.7).

Figure 8.7. Setting aliases can keep you from typing long names and code.


To set an alias with alias:

1.
Choose the appropriate file to edit, depending on which shell you're using.

  • bash users should use ~/.bashrc.

  • ksh users should use ~/.kshrc.

  • csh users should use ~/.cshrc.

If you don't have the appropriate file, use a different configuration file.

2.
vi .bashrc

Edit the configuration file you've selected.

3.
alias quit="logout"

Type alias followed by the term you want to use as the alias, =, and the command you're making an alias for (in quotes). Here, we're setting the word quit as an alias for the system command logout, so we can type quit instead of logout (Figure 8.7).

4.
Add as many other aliases as you want. See the sidebar called Good aliases to set in this section for more ideas.

5.
Save the file and exit from the editor.

See Chapter 4 for details about saving and exiting in vi and pico.

6.
su - yourid

Start a new login shell to test out the alias.

7.
alias

Type alias at the shell prompt for a listing of all the aliases you have defined (Code Listing 8.18).

Note

You can put aliases in other files, but it's customary to put them in the .bashrc (or other appropriate rc file, such as . kshrc or .cshrc), so they'll be set automatically when you log in, rather than having to be manually set.


Note

You can also issue alias commands from the shell prompt to set aliases for the current session.


Note

Be sure to make a backup copy of any configuration files you plan to change before you change them. That way, if you mess up, you still have the original file to work with.


Good aliases to set

Here are a few aliases that you might find it worthwhile to set on your system:

  • alias rm="rm -i" causes the system to prompt you about all deletions.

  • alias quit="logout" lets you use quit as a synonym for logout.

  • alias homepage="lynx http://www.raycomm.com/ lets you use homepage to start the lynx browser and connect to the Raycomm home page (substitute your home page as necessary).

Or, if you're coming from a DOS background, you might find the following aliases handy:

  • alias dir="ls" lets you use dir to list files.

  • alias copy="cp" lets you use copy to copy files.

  • alias rename="mv" lets you use rename to move or rename files.

  • alias md="mkdir" lets you use md to make a directory.

  • alias rd="rmdir" lets you use rd to remove a directory.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.37.20