Previous Table of Contents Next

C Shell
The following list describes the files the csh command uses.
standard input Reads from the terminal keyboard or the output of a pipe.
standard output Writes to the terminal screen or the input of a pipe.
standard error Writes errors to the terminal screen or the input of a pipe.
~/.cshrc Executes the commands stored in this file at start of execution by each new shell.
~/.login Executes the commands stored in this file during login to set up user-defined environment. Executed after /etc/login.
~/.logout Executes the commands stored in this file at logout time.
/etc/login Executes the commands stored in this file after ~/.cshrc.
/tmp/sh* Temporary file used for the '<<' command.
/dev/null The system trash can, used to dump unwanted output.
/etc/passwd Used to find home directories for ~name substitutions.
/bin/ksh Used to execute shell scripts that do not begin with a number sign (#) in row 1 column 1.


The shell returns a nonzero exit code if an error is incurred during noninteractive execution (shell script execution). If the shell is being used interactively, then the exit code of the last command is returned. The value of the exit code is stored in the $? variable.


There are two primary uses of the ksh program. The most common is using the shell as a command processor. Where ksh reads input from your terminal and processes it as commands. This is referred to as an interactive shell.

The second most common use of the shell is as a programming language. Using the shell as a programming language is beneficial. Shell scripts can be written to store multiple commands in a file. Programming constructs are provided to write a high level language type program. By using the shell, programming projects can be prototyped in a short time frame.

The shell provides a command environment. It is often desirable to change the environment variables for temporary use and then return to the original environment. Because the current shell passes the environment to all child processes, you can start another ksh to change your environment. Once the new shell is started you can change the environment, execute the desired commands, and then exit back to your original command processor (ksh). Your original environment exists just as you left it.


In this activity you use the ksh (csh) command as a command processor to enter commands from your terminal, an interactive programming language, and to create a new environment to execute commands. Begin at the shell prompt.

1.  Type env and press Return. Notice the ksh's environment is displayed on your terminal screen. Your environment is composed of variables that have been exported and can be passed to child processes.
    cj> env

C Shell
If you are using the csh use printenv.

2.  Type the following text; press Return at the end of each line. This is a shell script or program performed in interactive mode. You could store these commands in a file and execute the script as a command.
    cj> OIFS=$IFS      # save IFS in OIFS to restore later
    cj> IFS=":"        # reset the internal field descriptor to colon
    cj> cat db/phone | while read LINE
    set - $LINE
    echo "$1 \t $4 \t $5"
    cj> IFS=$OIFS      # restore the original IFS variable

The line beginning with cat pipes the db/phone into a while loop structure. The read LINE reads each line of the file and performs the set and echo command for each line. The set command sets each colon-separated field in db/phone to a positional parameter. The echo command then displays selected fields onto your screen. The result is a simple formatted output of the db/phone file.

C Shell
1.  There is no easy or reliable way to duplicate this code in the csh. But you can reset the command line arguments using the set command. Simply type the following.
    cj> set argv=( newarg1 newarg2 newargN )
2.  Now type echo $argv[$#argv] and press Return.
3.  Type ksh (csh) and press Return. Notice a prompt returns. A new shell has been executed by your login shell. If you do not have ksh available use sh or csh.
4.  Type HOME=/tmp and press Return. Your HOME directory is now /tmp.

C Shell
Type set home=/tmp.

5.  Type cd and press Return. Your new directory is /tmp.
6.  Type pwd and press Return to display your new HOME directory.
7.  Type export VAR=: and press Return. This exports the VAR variable in the new shell's environment.

C Shell
Type setenv VAR.

8.  Type env and press Return to display your new environment. Notice the HOME and VAR variables reflect their new values. If you like, continue working in this new environment.

C Shell
Type printenv.

9.  To return to your original shell, type exit and press Return or press Ctrl-D.
10.  Type env and press Return to display your login environment. Notice your HOME variable is set to /u/ts/mylogin and the VAR variable does not exist. Remember, exported variables are passed to child processes but not to parent processes.

C Shell
Type printenv.

11.  Turn to Module 23 to continue the learning sequence.

Previous Table of Contents Next