Command Aliases


Aliases are a very convenient feature introduced in csh and supported by tcsh, ksh, and bash. A command alias is a word linked to a block of text that is substituted by the shell whenever that word is used as a command. You can use aliases to give command names that are easier for you to remember or to type, and to automatically include particular options when you run a command.

The syntax for defining aliases varies slightly according to the shell you are using. In the C shell and extended C shell, the following alias lets you type lg as a substitute for the longer command ls -g:

 alias lg ls -g                       # csh or tcsh

In the Korn shell or bash, the same alias would be

 alias lg="ls -g"                       # bash or ksh

In either case, where you enter the command

 % lg

the shell replaces the alias lg with the full text of the alias, so the effect is exactly the same as if you had entered this:

 % ls -g

To see a list of the aliases you have defined, type the command alias by itself.

Aliases in ksh and bash

A valuable use of aliases is to automatically include options when you issue a command. For example, in Chapter 3 you saw that using the -i (interactive) option to the commands mv, cp, and rm can prevent you from accidentally deleting or overwriting files. By adding following lines to your .kshrc or .bashrc file, you can redefine those commands so that they always run with the -i option:

 alias rm="rm -i" alias mv="mv -i" alias cp="cp -i"

Note that, just as when you assign a variable, you must put quotes around any values that include spaces, as in “rm -i”.

Should you decide to redefine a command name like this and later discover that you need to use the command without the aliased options, you have two choices: you can temporarily unalias the command

 $ unalias rm

or you can use the full pathname of the command (found using the which command)

 $ /bin/rm

Alternately, of course, you could choose an alias that isn’t a command name, such as

 alias cpi="cp -i"

You can use aliases with command pipelines, as in

 $ alias wg="who | grep"

which would allow you to type

 $ wg dbp

instead of

 $ who | grep dbp

Aliases in csh and tcsh

Aliases can be used to automatically include options when you issue a command. In Chapter 3 you saw that using the -i (interactive) option to the commands mv, cp, and rm can prevent you from accidentally deleting or overwriting files. You can redefine those commands by aliasing them so that they always run with the -i option:

 alias rm rm -i alias mv mv -i alias cp cp -i

As with variables, aliases must be defined each time you start the shell. To save aliases that you want to use every time you log in, add them to your .cshrc or .tcshrc file.

If you redefine a command name with an alias and later discover that you need to use the command without the aliased options, you have two choices: you can temporarily unalias the command

 $ unalias rm

or you can use the full pathname of the command

 $ /bin/rm

An alternative would be to choose an alias that isn’t a command name, such as

 alias cpi cp -i

There are many more uses for aliases. You could define

 $ alias wg 'who grep'

which would allow you to type

 $ wg dbp

instead of

 $ who | grep dbp

Note that in this example you must include quotes (') around the alias. If you do not, the shell will assign the alias wg to the command who and then try to pipe the output to grep.




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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