Chapter IV. Aliases and Variables in csh

[Index] [Next] [Prev]

Covered: alias unalias ; ' " echo csh set $ unset

Another nifty feature of Unix is "aliases". In the C-shell, an alias is a way of defining one command to be "shorthand" for another. For example, suppose you type "cat .login" a lot. To be able to do it easier, you can type
% alias cl cat .login
This defines a new command, "cl". When you type "cl", the computer acts just as if you had typed "cat .login". Try it out.

The alias command has 3 forms. In the above form, it defines a new alias. Now, you can type

% alias cl
to see what "cl" is defined to be. You can also type
% alias
"alias" by itself gives you a list of all the currently-defined aliases in csh.

To get rid of an alias, the command is "unalias". Type

% unalias cl
If you subsequently type "cl", you should get some sort of error message, and "alias cl" will produce no result.

Note: "alias" and "unalias" are two of the few actual csh built in commands. When you type "alias" you are not running a program called "alias". Rather, you are executing a routine that is part of csh.

Suppose you would like "cl" to do "cat .login" twice. The C-shell does have a way to enter two commands on the same line. You do this by separating the commands with ";". For example

% cat .login ; cat .login
will print the contents of your ".login" file twice. However,
% alias cl cat .login ; cat .login
does not do what we want. If you try it, you find that it is treated as two separate commands: "alias cl cat .login" and "cat .login". The first defines "cl", while the second prints your .login file immediately. To avoid ambiguities like this, you can use quotes. Thus, you could say
% alias cl "cat .login ; cat .login"
Single quotes also work:
% alias cl 'cat .login ; cat .login'
Now, "alias" typed from the keyboard is not really all that useful. What is helpful is a more permanent alias. You can make an alias "permanent" by putting the alias command in your .cshrc file. Since all the commands in the .cshrc file are executed every time csh is started up, you then have your newly-defined command available every time you log in.

It is likely there are already some aliases in your .cshrc file. A very common one is "alias ls ls -sF". This makes "ls" shorthand for a longer version of itself, which prints the size of a file, along with a special character that tells what type of file it is ("/", meaning directory, and "*", meaning executable, are the most useful of these).

Putting aliases in .cshrc should be done with care, since modifying such an important file can be dangerous. After all, what if you destroy the file? Also, if you don't know how to use an editor, how to you get the alias out of the file?

One solution to this is to make a backup of .cshrc, by typing

% cp .cshrc cshrc.bak
for example. Then if things get messed up, you can just type "cp cshrc.bak .cshrc", log out, and all will be well.

Another way to do this would be to create a special directory for backups: "mkdir backup". Then you can make a backup of .cshrc by typing "cp .cshrc backup" and restore it by typing "cp backup/.cshrc ~".

However you choose to do it, make a backup of .cshrc now, so that you can modify it safely. Now, let's define a new command. Say we want to define the command "xx" to print a short message on the screen. First, we need to know how to print messages on the screen. We could put a message in a text file and then use "cat" to print the text file. However, there is a command that prints short strings of text. This command is "echo". For example, type

% echo Howdy
Unix should oblige and print "Howdy". Now, we can define "xx". Type
% cat >> .cshrc
Don't forget the ">>", which appends, as opposed to ">", which overwrites. Now, you are adding lines to .cshrc. Type
alias xx echo Howdy
and then press . If you did something wrong, restore your old copy of .cshrc and try again. Now, type
% more .cshrc
There is your new alias command stuck on the end. Type
% xx
The reason it doesn't work is that "xx" is not yet defined. It has been added to .cshrc, but .cshrc has not been executed since it was added. There are several ways to cause .cshrc to be executed. One is to log out and then log back in. Another is to execute another copy of csh. You can do this because csh is just a program like any other program, and typing its name will execute it. Type
% csh
Now you have another copy of csh running. .cshrc was executed (note: however, .login was not). Type
% xx
You should get a friendly "Howdy" this time. The problem facing us now is how to exit this copy of csh. If you type "logout", the computer will inform you that this is not your login shell. In this case, you can exit csh by typing . You will exit to the original copy of csh, and "xx" will no longer be defined. At this point, you may also want to restore your original ".cshrc".

Aliases are useful for defining new commands in csh, but there are other things one may want to define. For example, suppose you often access a directory called "/biglongdirectoryname" (for some unknown reason). That is a lot to type, but, since it is not a command, we cannot use an alias to make it easier. For purposes like this, csh has variables.

Let's put that directory name in the variable "bldir". First, type

% echo $bldir
This tells csh to print the value of bldir. It should tell you that bldir is not defined. This is good. Many csh variables are used by the system, and we do not want to change one of these accidentally. Since bldir is not defined, it probably isn't one of these. To change the value of a variable, the syntax is "set var = ....". So, to set bldir equal to the name of that directory, you can type
% set bldir = /biglongdirectoryname
Whenever you use the value of a variable, you preceed it with "$". Thus, to find out what is in bldir, type
% echo $bldir
This should print "/biglongdirectoryname". Now, to get a listing of what is in this directory, you can type
% ls $bldir
Of course, that directory probably doesn't exist on your system. Try the above again with the name of one that does.

To set your working directory to this directory, you can type "cd $bldir", and so on. In general, variables can be mixed with all other sorts of csh syntax. For example, if /biglongdirectoryname had a subdirectory called "test", you could list its contents by typing "ls $bldir/test". Another way to define the "xx" command to print "Howdy" would be

% set hd = Howdy
% alias xx echo $hd
Quotes can also be used to avoid ambiguity with "echo" and "set". For example, you can type
% echo "Howdy ; alias xx qqqqq"
or
% set bldir = "x ; y"
and so on.

If you want to make a defined variable undefined, the command is "unset". Thus

% unset bldir
puts things back the way they were. However, there really isn't any good reason to do this right now, since variables go away when you log out.

You can also put any of these commands into ".cshrc". If you want a friendly greeting each time you log in, add "echo Welcome to Unix" to your .login file.

Earlier, I mentioned that the system uses csh variables for its own purposes. Some of these are "path", "prompt" and "home".

When you type a command in the C-shell, it will try to find a file with that name and execute it. Most of these files are not in your home directory. Thus, csh needs to know where to look for them. This is what "path" is for. "path" holds a list of directories in which to look for executable files. Type

% echo $path
There is the list. Note that one of them is ".". This indicates that csh will look in your current working directory for executable files. If "." were not in the list, it would not do so.

"prompt" simply holds the current csh prompt. Type

% echo $prompt
to see that this is so.

Finally, "home" holds the name of your home directory, which you can easily verify. Of course, this is not all that useful, since "~" can also be used for that purpose.


Unix Tutorial (September 1994 version) by Glenn Chappell <ggc@uiuc.edu> (Feel free to distribute this document however you want.)
[Index] [Next] [Prev]