Unix Concepts   «Prev  Next»
Lesson 8Storing your C shell settings
Objective .cshrc file to store alias, history, and prompt definitions.

Storing your C shell Settings

You have been customizing your environment by entering changes at the command line.
But anything you do at the command line applies only to your current session. Once you log out, any aliases you changed, along with the history and prompt definitions, are lost. To make your custom settings permanent, you must store them in a configuration file in your home directory. In the C shell, this file is called .cshrc.
  • .cshrc file
    Every time you log in, UNIX reads your .cshrc file and processes the commands it contains. Typically, this file is where you define aliases and C shell variables. The following MouseOver presents a sample .cshrc file. It is assumed that the .cshrc file was created during your current UNIX session. As a result, none of the new settings would be in effect. They would take effect only after your next log in.

Function of .login and .cshrc files in Unix

.cshrc is read every time you log in, but that is not quite true.
The .cshrc file is read every time you start another C shell. The two situations are often the same, but sometimes they are not. For example, suppose you are working in a graphical user interface (GUI). Typical UNIX GUIs let you work in multiple terminal windows. If we assume your account uses the C shell, then every time you open a terminal window, you’d be starting another C shell. Along with the .cshrc file, the C shell uses another configuration file called .login. Unlike .cshrc, the .login file is read only once per session, when you log in. The .login file is where you define general account features such as initializing your terminal or setting environment variables. Here is another tip. You do not really need to log out and log in again to process the commands in .cshrc. To read your .cshrc file immediately, use the source command:
% source .cshrc

This command tells the shell to read your .cshrc and immediately run the commands it contains.
csh /etc/csh.cshrc, followed by /etc/csh.login, /etc/.login, /etc/login.std, or
/etc/cshrc, depending on the operating system 
After that, ~/.cshrc and ~/.login

Example
1 $ pathname=/home/lilliput/jake/.cshrc
2 $ print ${pathname#/home}
/lilliput/jake/.cshrc
  1. The local variable pathname is assigned /home/liliput/jake/.cshrc.
  2. The # removes the smallest leading portion of pathname containing the pattern /home; that is, /home is stripped from the beginning of the path variable.

Sample Shell login script .cshrc File

Sample Cshrc
set history=100

  1. cat .cshrc command: The cat command displays the contents of the .cshrc file. This file should reside in your home directory.
  2. alias output block: Several aliases have been defined. These aliases will be available at log in and throughout your UNIX session, unless you disable any of them with unalias.
  3. set history command: The history variable is set to 100. This means that command history will be available, as soon as you log in, for the 100 most recent commands.
  4. set prompt command: The prompt variable defines a prompt, What next?, that will take effect at your next log in, unless you change the prompt at the command line.
  5. last lab1% prompt: Note that your current prompt is unaffected. Even though you defined a different prompt in .cshrc, the new prompt will not take effect until UNIX reads the .cshrc file, at your next login.

Configuration files like .cshrc are one place where you can store commands in a file.

Unix OS Design

Storing your C Shell Settings - Shell Scripts

A shell script is a sequence of commands stored in a file. When you type the file name at the command line, you run the script as if it’s a new UNIX command. Shell scripts are similar to batch files on Windows or DOS machines. The simplest scripts contain everyday UNIX commands, but scripts also understand their own programming language syntax. This syntax allows the use of variables, conditional execution (if-then logic), and repeated processing (looping).
As a simple example, suppose you’ve created a file named myscript that looks like this:
% cat myscript 
date 
who 
pwd 
ls –F

File must be executable

For this file to work as a script, it must be executable. That means the author must have execute permission, so you change the access mode as follows:
% chmod u+x myscript

Now you can run the commands in the script by entering myscript at the command line:
% myscript

Sometimes when you run a shell script, you encounter a “Command not found” error. That’s because UNIX searches a particular set of directories when locating a command. The “Command not found” message means that the shell script is not in any of the places UNIX is searching. In the C shell, the path variable determines which directories will be searched. Configuration files, such as .cshrc, are a special type of shell script. A configuration file is executed automatically, so you do not need execute permission on it.
In the next lesson, this module will be concluded by reviewing key commands, terms, and concepts you have learned.

Storing CShell Settings - Exercise
Click the Exercise link below to practice creating a .cshrc file.
Storing CShell Settings - Exercise

SEMrush Software 8 SEMrush Banner 8