next up previous contents
Next: Variables Up: The C Shell Previous: Command History


Now it is time to start exploring the power of the C shell. This is the point where the C shell really starts to stand out over the Bourne shell. The C shell enables the user to define complicated, or not, commands in terms of easy to type aliases. These aliases can save a heavy user litteraly hours of typing. As a simple example, take the command for getting a long listing of files in a directory, ls -l. While this is not very difficult to enter, it can be simplified using an alias:

% alias ll ls -l

After this, for the rest of the current session, typing ll would result in the long listing of the current directory being displayed. The alias could even be made permanent for every session by including it in a login file, but that will be covered in later. A list of active aliases is kept by the shell. To see a list of aliases the user would type alias with no arguments. To check a particular alias, the alias command would be entered with the name of the alias in question as the only argument. If the alias does not exist, the prompt will simply reappear on the next line. The following session demonstartes the use of the alias command:

% alias ll ls -l
% alias bye exit
% alias
ll     ls -l
bye    exit
% alias bye

To remove an alias, one simply types unalias followed by the alias to be removed:

% alias ll
ls -l
% unalias ll
% alias ll

More complicated aliases can be designed using the techniques outlined in earlier sections (like command grouping and escaping special characters). When using an alias remeber that the shell will process the alias at that time. This can be a bit of a problem if one is not careful. The following example illustrates the problem:

% alias ll ls -l d*.*
% alias ll
ls -la data1.95 data2.95 doc.tex
% cd ~another_usr
% ll
/bin/ls: data1.95: No such file or directory
/bin/ls: data2.95: No such file or directory
/bin/ls: doc.tex: No such file or directory

The problem can be fixed by simply enclosing the definition in quotes. Here, single or double will work as no variable substitutions are made. When the definition is not enclosed in quotes the definition is taken as exact by the shell. This means that it uses the listing of the current directory - always. When in another directory, the shell looks for files that were in the directory the alias was created in. This may seem a bit confusing, but it really comes down to the order in which expansions and substitutions are executed during the aliasing process. It never hurts to enclose things in quotes (unless a particular special character is required) so it is wise to use quotes in aliases as often as possible.

Aliases can also be used to redefine current commands. The C shell will not prevent a user from renaming an alias after say, ls. This means that one could redefine ls to behave in similar or completely different in behaviour. A good use of this property is as follows:

% alias rm rm -i

which would cause the shell to prompt the user on the deletion of each file given as an argument. Never a bad idea. Redefining commands using alias may seem dangerous, but keep in mind that these aliases can be removed as well using the unalias command.

next up previous contents
Next: Variables Up: The C Shell Previous: Command History

Douglas M Gingrich
Mon Apr 27 15:25:49 MDT 1998