Abbreviations with Aliases



next up previous contents index
Next: A Word about Up: Personalizing Unix Previous: Setting Terminal Characteristics

Abbreviations with Aliases

You may find after a while that you are giving the same series of commands over and over, and if that seems like needless repetition, it is. One approach, described in Chapter 5, Unix Tools, uses ``job control'' to repeat previous commands. The approach described here uses ``aliases'' which let you create nicknames for commands or change the name of a command to something you became familiar with in a previous existence (such as del and dir). Like environmental variables, aliases are defined in the .cshrc or .kshrc file in your home directory. For example, if you find yourself frequently typing ls -Fas, you can set up the alias dir for this command:   

$ alias dir='ls -Fas'   	In Korn shell.   
% alias dir 'ls -Fas'   	In C shell.   

After the system's login procedure reads all your . (dot) files, typing dir acts exactly as if you had typed ls -Fas.

To determine the definition of a command, use alias with no definition:

% alias dir   	Check the alias for dir.   
dir=ls -Fas   	Unix reveals your secret.   
% alias   	See all aliases.   
j       (jobs -l)   	   
l       (ls -F)   	   
ll      (ls -lC)   	   
ls      (ls -F)   	   

Note that when we got tricky and gave the alias command with no argument, the shell interpreted that as a question and told us all the aliases we had already defined.

You can also move command arguments in csh and thus make it possible to string several commands together in a single alias. A sample use of this procedure is to combine cd and ls into a single command which tells you where you are as soon as you get there. To move the arguments of the cd command in the alias definition, use \!* . This is a special variable meaning ``all the arguments given to the command'':  

alias cd 'cd \!* ;ls -CF'   	Combination cd and list.   

Note the escape character before ! keeps the shell from thinking ! is a command.



next up previous contents index
Next: A Word about Up: Personalizing Unix Previous: Setting Terminal Characteristics