Aliases


To use aliases, you must first understand what an alias is. An alias is a file that represents another object in the file system. For example, if you specify a file that just says grepnow, inside it you may have a complex grep command with REs (regular expressions) and/or many options. Typing grepnow at the shell prompt will execute that file's contents, which will be the longer command. What would you rather type out?

 grepnow grep "[:digit:]\{3\}[ -]\?[:digit:]\{4\}" file 

The choice is clear. Using aliases can really save you time on frequently entered commands, especially if they are long in syntax.

Suppose you want to match a specific number of repetitions of a pattern. A good example is a phone number. You could easily search for a 7-digit phone number using this methodology. One of the most useful features that shells provide to the user is the capability to create command aliases. As you can see, the commands can get completely out of hand as you use REs and options to build it out and make it more effective.

Aliases are, quite simply, aliases. If, for example, you're a longtime DOS user, you might find yourself typing del instead of rm to delete files. Want to change this? Just set an alias with the alias command. The syntax is as follows: alias <newname> <command to run>. If you actually want to make that alias for del, type the following: alias del rm. After setting this alias, any time you type del, it executes the command rm. This is actually something commonly done on many different systems.

More useful than simply renaming commands, you can use the alias command to create meta commands that enforce certain options. For example, it's always a good idea to force rm to be in interactive mode (rm -i). To do this with an alias, you can type the following: alias rm 'rm -i'. (Note the direction of the single quotes.) Now when you type rm it actually executes rm -i instead.

Remember, aliasing is done to help you shorten your typing workload. If you find yourself in situations where you are typing out long strings often and they all start to look identical, then it would make sense to create aliases to save you some time.



    SAMS Teach Yourself Unix in 10 Minutes
    Sams Teach Yourself Unix in 10 Minutes (2nd Edition)
    ISBN: 0672327643
    EAN: 2147483647
    Year: 2005
    Pages: 170

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net