[ Team LiB ] Previous Section Next Section

The C Shell

The C shell, written by Bill Joy when he was at UC Berkeley, is popular with many users of Berkeley UNIX. The C shell is completely different from the Bourne and Korn shells and has its own syntax. The most important advantages of the C shell are command history, command editing, and aliases. Command history stores a record of the most recent commands that you have used. You can display these commands and reuse them as originally issued. Command editing enables you to change a command by editing it. Aliases let you type short names for frequently used commands. You can also combine sequences of frequently used commands and provide an alias for the sequence.

Reviewing C Shell Initialization Files

The C shell uses two initialization files in the user's home directory to set the user's environment: .login and .cshrc (C shell run control).

When you log in, the .login file is read, and then the .cshrc file. When you start the C shell from a command line, only the .cshrc file is read.

Defining C Shell Environment Variables

To define an environment variable for the C shell, type setenv VARIABLE value and press Return.


oak% setenv DISPLAY rogue:0
oak%



Creating Aliases for the C Shell

Define any aliases for the user in the .cshrc file. The syntax for creating an alias is alias alias-name command-sequence. For example, if you frequently use the ftp command to send batches of files and don't want to be prompted for each file, you can create an alias for the ftp -i command to turn off interactive prompting. When you add the following line to your .cshrc file, ftp is started with interactive prompting turned off.


alias ftp "ftp -i"

The following example shows aliases from a .cshrc file. Note that if the command contains spaces, you enclose the entire command in quotes. Both double and single quotes are used in the following examples.

graphics/new.gif

NOTE. Double quotes enable variables to be interpolated and single quotes don't.



alias a alias
a h history
a c clear
a lf ls -F
a ll "ls -l | more"
a la ls -a
a s "source .cshrc"
a f 'find ~ -name core -print'
a copytotape "tar cvf /dev/rmt/0 *"
a ftp "ftp -i"

graphics/new.gif

After you have made changes to a .cshrc file, the changes are not recognized unless you source the .cshrc file by typing source .cshrc or until you log out and log in again. When you source the .cshrc file in a shell, the changes are recognized only in the current shell and any other shells and programs invoked from that shell.

Setting the History Size for the C Shell

graphics/new.gif

The default is for the C shell to save only the most recent command in its history list. You can change the number of commands the shell saves. To set history for the C shell, on a command line type set history= n and press Return. history is set to the number of lines you specify.


oak% set history=10
oak%

You can set history temporarily for a shell window or set it "permanently" so that the same history setting is available at each login session. Enter the command as a line in your .cshrc file and type source .cshrc.

Using history for the C Shell

To display the history for the C shell, on a command line type history and press Return. The last n commands that you had set for the history are displayed.


oak% history
    26  pwd
    27  kermit
    28  cd Howto
    29  tar xvf /dev/rmt/0
    30  ls -l howto*
    31  cd
    32  cd Config/Art
    33  ls -l
    34  tar cvf /dev/rmt/0
    35  history
oak%

To repeat the previous command in a C shell, type !! and press Return. The previous command is executed again.


oak% history
    26  pwd
    27  kermit
    28  cd Howto
    29  tar xvf /dev/rmt/0
    30  ls -l howto*
    31  cd
    32  cd Config/Art
    33  ls -l
    34  tar xvf /dev/rmt/0
    35  history
oak% !!
history
    27  kermit
    28  cd Howto
    29  tar xvf /dev/rmt/0
    30  ls -l howto*
    31  cd
    32  cd Config/Art
    33  ls -l
    34  tar xvf /dev/rmt/0
    35  history
    36  history
oak%

To repeat the last word of the previous command in a C shell, type !$ and press Return. The last word from the previous command is used as part of the command-line argument.

For example, you might list the complete path name of a file and then use the path name as the argument to edit the file with vi or to print it.


oak% ls -l /home/ignatz/quest
-rw-r--r--   1 ignatz   staff               24 Jul 16 15:07 quest
oak% lp !$
lp /home/ignatz/quest
oak%

You can use the !$ command anywhere within the command line. In the following example, the file /home/ignatz/quest is copied to the /tmp directory.


oak% ls -l /home/ignatz/quest
-rw-r--r--   1 ignatz   staff             24 Jul 16 15:07 quest
oak% cp !$ /tmp
cp /home/ignatz/quest /tmp
oak%

To repeat a numbered command in a C shell, type ! n and press Return. The number in the shell prompt is n. The command is executed again.


oak% history
29  tar xvf /dev/rmt/0
30  ls -l howto*
31  cd
32  cd Config/Art
33  ls -l
34  tar xvf /dev/rmt/0
35  ls -l
36  cd
37  lp howto*
38  history
oak% !32
cd Config/Art
oak%



Incorporating a New Command for the C Shell (rehash)

The C shell keeps an internal record of the location of all executable files that are found in the directories named in the path variable. This internal record is called a hash table. When you add a new command to any of the directories in your path, the C shell cannot see it until you use the rehash command to refresh the hash table. Any new commands are then incorporated into your command search path.


oak% newcommand
newcommand: Command not found
oak% rehash
oak% newcommand
Command output
oak%



Editing C Shell History Commands

You can edit commands retrieved from the history list by using the s/ oldstring/ newstring/ form to substitute the retrieved string in the command. In the following example, an incorrectly typed command from the history list is corrected.


oak% history
    31  cd
    32  ls
    33  cd /home/frame3.1
    34  ls
    35  cd ..
    36  tar cvf /dev/rmt/0 frame3.1
    37  lp questionnaire
    38  lpstat -t
    39  echo $PaTH
    40  history
oak% !39:s/a/A/
echo $PATH
.:/home/winsor:/usr/openwin/bin:/usr/deskset/bin:/home/
 winsor/bin:/bin:/home/bin:/etc:/usr/etc:/usr/bin:/home/ frame3.1/bin
oak%

graphics/new.gif

An excellent C shell programming reference is Using csh and tcsh, by Paul DuBois, O'Reilly & Associates, 1995.

    [ Team LiB ] Previous Section Next Section