








                      AAnn IInnttrroodduuccttiioonn ttoo tthhee CC sshheellll


                                _W_i_l_l_i_a_m _J_o_y
                    _(_r_e_v_i_s_e_d _f_o_r _4_._3_B_S_D _b_y _M_a_r_k _S_e_i_d_e_n_)
                         Computer Science Division
         Department of Electrical Engineering and Computer Science
                    University of California, Berkeley
                        Berkeley, California 94720


                                 _A_B_S_T_R_A_C_T

               _C_s_h is a new command language interpreter for sys-
          tems.   It  incorporates  good features of other shells
          and a _h_i_s_t_o_r_y mechanism similar  to  the  _r_e_d_o  of  IN-
          TERLISP.   While  incorporating  many features of other
          shells  which  make  writing  shell   programs   (shell
          scripts) easier, most of the features unique to _c_s_h are
          designed more for the interactive UNIX user.

               UNIX users who have read a general introduction to
          the  system  will  find a valuable basic explanation of
          the shell here.  Simple terminal interaction  with  _c_s_h
          is  possible  after  reading  just the first section of
          this  document.   The  second  section  describes   the
          shell's  capabilities  which  you can explore after you
          have begun to become acquainted with the shell.   Later
          sections  introduce  features which are useful, but not
          necessary for all users of the shell.

               Additional information includes an appendix  list-
          ing  special  characters of the shell and a glossary of
          terms and commands introduced in this manual.


     IInnttrroodduuccttiioonn

          A _s_h_e_l_l is a command language interpreter.  _C_s_h is the  name
     of  one particular command interpreter on UNIX.  The primary pur-
     pose of _c_s_h is to translate command lines  typed  at  a  terminal
     into  system  actions, such as invocation of other programs.  _C_s_h
     is a user program just like any you might write.  Hopefully,  _c_s_h
     will  be  a  very  useful program for you in interacting with the
     UNIX system.

          In addition to this document, you will want to  refer  to  a
     copy of the UNIX User Reference Manual.  The _c_s_h documentation in
     section  1  of the manual provides a full description of all fea-
     tures of the shell and is the definitive reference for  questions
     about the shell.









     USD:4-2                            An Introduction to the C shell


          Many words in this document are shown in _i_t_a_l_i_c_s.  These are
     important  words; names of commands, and words which have special
     meaning in discussing the shell and UNIX.  Many of the words  are
     defined  in a glossary at the end of this document.  If you don't
     know what is meant by a word, you should look for it in the glos-
     sary.

     AAcckknnoowwlleeddggeemmeennttss

          Numerous people have provided good input about previous ver-
     sions of _c_s_h and aided in its debugging and in the  debugging  of
     its  documentation.   I  would  especially  like to thank Michael
     Ubell who made the  crucial  observation  that  history  commands
     could be done well over the word structure of input text, and im-
     plemented  a  prototype  history mechanism in an older version of
     the shell.  Eric Allman has also provided a large number of  use-
     ful  comments on the shell, helping to unify those concepts which
     are present and to identify and eliminate useless and  marginally
     useful  features.   Mike  O'Brien  suggested the pathname hashing
     mechanism which speeds command execution.  Jim Kulp added the job
     control and directory stack primitives and added their documenta-
     tion to this introduction.









































     An Introduction to the C shell                            USD:4-3


     11..  TTeerrmmiinnaall uussaaggee ooff tthhee sshheellll

     11..11..  TThhee bbaassiicc nnoottiioonn ooff ccoommmmaannddss

          A _s_h_e_l_l in UNIX acts mostly as a medium through which  other
     _p_r_o_g_r_a_m_s  are  invoked.  While it has a set of _b_u_i_l_t_-_i_n functions
     which it performs directly, most commands cause execution of pro-
     grams that are, in fact, external to the  shell.   The  shell  is
     thus distinguished from the command interpreters of other systems
     both  by the fact that it is just a user program, and by the fact
     that it is used almost exclusively as a  mechanism  for  invoking
     other programs.

          _C_o_m_m_a_n_d_s  in the UNIX system consist of a list of strings or
     _w_o_r_d_s interpreted as a _c_o_m_m_a_n_d _n_a_m_e followed by _a_r_g_u_m_e_n_t_s.   Thus
     the command

          mail bill

     consists  of two words.  The first word _m_a_i_l names the command to
     be executed, in this case the mail program which  sends  messages
     to  other  users.   The shell uses the name of the command in at-
     tempting to execute it for you.  It will look in a number of  _d_i_-
     _r_e_c_t_o_r_i_e_s for a file with the name _m_a_i_l which is expected to con-
     tain the mail program.

          The  rest of the words of the command are given as _a_r_g_u_m_e_n_t_s
     to the command itself when it is executed.  In this case we spec-
     ified also the argument _b_i_l_l which is  interpreted  by  the  _m_a_i_l
     program  to be the name of a user to whom mail is to be sent.  In
     normal terminal usage we might use the _m_a_i_l command as follows.

          % mail bill
          I have a question about the csh documentation.
          My document seems to be missing page 5.
          Does a page five exist?
               Bill
          EOT
          %

          Here we typed a message to send to _b_i_l_l and ended this  mes-
     sage  with  a  ^D  which sent an end-of-file to the mail program.
     (Here and throughout this document, the notation ``^_x'' is to  be
     read ``control-_x'' and represents the striking of the _x key while
     the  control key is held down.)  The mail program then echoed the
     characters `EOT' and transmitted our message.  The characters  `%
     '  were printed before and after the mail command by the shell to
     indicate that input was needed.

          After typing the `% ' prompt, the shell was reading  command
     input  from  our  terminal.   We  typed  a complete command `mail
     bill'.  The shell then executed the _m_a_i_l  program  with  argument
     _b_i_l_l  and went dormant waiting for it to complete.  The mail pro-
     gram then read input from our terminal until we signaled an  end-









     USD:4-4                            An Introduction to the C shell


     of-file  via  typing a ^D after which the shell noticed that mail
     had completed and signaled us that it was ready to read from  the
     terminal again by printing another `% ' prompt.

          This  is  the essential pattern of all interaction with UNIX
     through the shell.  A complete command is typed at the  terminal,
     the shell executes the command and when this execution completes,
     it prompts for a new command.  If you run the editor for an hour,
     the  shell will patiently wait for you to finish editing and obe-
     diently prompt you again whenever you finish editing.

          An example of a useful command you can execute  now  is  the
     _t_s_e_t command, which sets the default _e_r_a_s_e and _k_i_l_l characters on
     your terminal - the erase character erases the last character you
     typed  and the kill character erases the entire line you have en-
     tered so far.  By default, the erase character is the delete  key
     (equivalent  to `^?')  and the kill character is `^U'.  Some peo-
     ple prefer to make the erase character the backspace key (equiva-
     lent to `^H').  You can make this be true by typing

          tset -e

     which tells the program _t_s_e_t to set the erase character to tset's
     default setting for this character (a backspace).

     11..22..  FFllaagg aarrgguummeennttss

          A useful notion in UNIX is that of a _f_l_a_g  argument.   While
     many arguments to commands specify file names or user names, some
     arguments  rather  specify  an optional capability of the command
     which you wish to invoke.  By convention,  such  arguments  begin
     with the character `-' (hyphen).  Thus the command

          ls

     will  produce  a  list of the files in the current _w_o_r_k_i_n_g _d_i_r_e_c_-
     _t_o_r_y.  The option _-_s is the size option, and

          ls -s

     causes _l_s to also give, for each file the size  of  the  file  in
     blocks of 512 characters.  The manual section for each command in
     the  UNIX  reference  manual gives the available options for each
     command.  The _l_s command has a large number of useful and  inter-
     esting  options.   Most  other commands have either no options or
     only one or two options.  It is hard to remember options of  com-
     mands  which are not used very frequently, so most UNIX utilities
     perform only one or two functions rather than having a large num-
     ber of hard to remember options.

     11..33..  OOuuttppuutt ttoo ffiilleess

          Commands that normally read input or  write  output  on  the
     terminal  can also be executed with this input and/or output done









     An Introduction to the C shell                            USD:4-5


     to a file.

          Thus suppose we wish to save the  current  date  in  a  file
     called `now'.  The command

          date

     will print the current date on our terminal.  This is because our
     terminal  is the default _s_t_a_n_d_a_r_d _o_u_t_p_u_t for the date command and
     the date command prints the date on  its  standard  output.   The
     shell lets us _r_e_d_i_r_e_c_t the _s_t_a_n_d_a_r_d _o_u_t_p_u_t of a command through a
     notation  using  the  _m_e_t_a_c_h_a_r_a_c_t_e_r  `>' and the name of the file
     where output is to be placed.  Thus the command

          date > now

     runs the _d_a_t_e command such that its standard output is  the  file
     `now'  rather  than  the  terminal.  Thus this command places the
     current date and time into the file `now'.  It  is  important  to
     know  that the _d_a_t_e command was unaware that its output was going
     to a file rather than to the terminal.  The shell performed  this
     _r_e_d_i_r_e_c_t_i_o_n before the command began executing.

          One other thing to note here is that the file `now' need not
     have  existed  before  the  _d_a_t_e  command was executed; the shell
     would have created the file if it did not exist.  And if the file
     did exist?  If it had existed previously these previous  contents
     would  have  been  discarded!  A shell option _n_o_c_l_o_b_b_e_r exists to
     prevent this from happening accidentally; it is discussed in sec-
     tion 2.2.

          The system normally keeps files which you  create  with  `>'
     and  all other files.  Thus the default is for files to be perma-
     nent.  If you wish to create a file which will be  removed  auto-
     matically,  you  can  begin  its  name with a `#' character, this
     `scratch' character denotes the fact that  the  file  will  be  a
     scratch file.*  The system will remove such files after a  couple
     of  days,  or  sooner if file space becomes very tight.  Thus, in
     running the _d_a_t_e command above, we don't really want to save  the
     output forever, so we would more likely do

          date > #now




     -----------
     *Note  that  if your erase character is a `#', you will have
     to precede the `#' with a `\'.  The fact that the `#'  char-
     acter  is  the  old (pre-CRT) standard erase character means
     that it seldom appears in a file name, and allows this  con-
     vention  to  be  used for scratch files.  If you are using a
     CRT, your erase character should be a ^H, as we demonstrated
     in section 1.1 how this could be set up.









     USD:4-6                            An Introduction to the C shell


     11..44..  MMeettaacchhaarraacctteerrss iinn tthhee sshheellll

          The  shell  has  a  large number of special characters (like
     `>') which indicate special functions.  We say that  these  nota-
     tions  have _s_y_n_t_a_c_t_i_c and _s_e_m_a_n_t_i_c meaning to the shell.  In gen-
     eral, most characters which are neither letters nor  digits  have
     special  meaning to the shell.  We shall shortly learn a means of
     _q_u_o_t_a_t_i_o_n which allows us to use _m_e_t_a_c_h_a_r_a_c_t_e_r_s without the shell
     treating them in any special way.

          Metacharacters normally have effect only when the  shell  is
     reading  our  input.   We  need  not  worry  about  placing shell
     metacharacters in a letter we are sending via _m_a_i_l,  or  when  we
     are  typing in text or data to some other program.  Note that the
     shell is only reading input when it has prompted with `%  '  (al-
     though we can type our input even before it prompts).

     11..55..  IInnppuutt ffrroomm ffiilleess;; ppiippeelliinneess

          We  learned  above  how to _r_e_d_i_r_e_c_t the _s_t_a_n_d_a_r_d _o_u_t_p_u_t of a
     command to a file.  It is also possible to redirect the  _s_t_a_n_d_a_r_d
     _i_n_p_u_t  of  a  command  from  a file.  This is not often necessary
     since most commands will read from a file whose name is given  as
     an argument.  We can give the command

          sort < data

     to  run  the  _s_o_r_t command with standard input, where the command
     normally reads its input, from the file `data'.   We  would  more
     likely say

          sort data

     letting  the  _s_o_r_t  command open the file `data' for input itself
     since this is less to type.

          We should note that if we just typed

          sort

     then the sort program would sort lines from its  _s_t_a_n_d_a_r_d  _i_n_p_u_t.
     Since we did not _r_e_d_i_r_e_c_t the standard input, it would sort lines
     as  we typed them on the terminal until we typed a ^D to indicate
     an end-of-file.

          A most useful capability is the ability to combine the stan-
     dard output of one command with the standard  input  of  another,
     i.e.  to run the commands in a sequence known as a _p_i_p_e_l_i_n_e.  For
     instance the command

          ls -s

     normally produces a list of the files in our directory  with  the
     size  of  each in blocks of 512 characters.  If we are interested









     An Introduction to the C shell                            USD:4-7


     in learning which of our files is largest we  may  wish  to  have
     this sorted by size rather than by name, which is the default way
     in  which _l_s sorts.  We can look at the many options of _l_s to see
     if there was an option to do this, or instead we can use a couple
     of simple options of the _s_o_r_t command, combining it  with  _l_s  to
     get what we want.

          The  _-_n  option of sort specifies a numeric sort rather than
     an alphabetic sort.  Thus

          ls -s | sort -n

     specifies that the output of the _l_s command run with  the  option
     _-_s  is  to be _p_i_p_e_d to the command _s_o_r_t run with the numeric sort
     option.  This would give us a sorted list of our files  by  size,
     but  with  the  smallest first.  We could then use the _-_r reverse
     sort option and the _h_e_a_d command in combination with the previous
     command doing

          ls -s | sort -n -r | head -5

     Here we have taken a list of  our  files  sorted  alphabetically,
     each  with  the size in blocks.  We have run this to the standard
     input of the _s_o_r_t command asking it to sort  numerically  in  re-
     verse  order (largest first).  This output has then been run into
     the command _h_e_a_d which gives us the first  few  lines.   In  this
     case we have asked _h_e_a_d for the first 5 lines.  Thus this command
     gives us the names and sizes of our 5 largest files.

          The  notation introduced above is called the _p_i_p_e mechanism.
     Commands separated by `|' characters are  connected  together  by
     the  shell, and the standard output of each is run into the stan-
     dard input of the next.  The leftmost command in a pipeline  will
     normally take its standard input from the terminal and the right-
     most will place its standard output on the terminal.  Other exam-
     ples of pipelines will be given later when we discuss the history
     mechanism;  one important use of pipes which is illustrated there
     is in the routing of information to the line printer.

     11..66..  FFiilleennaammeess

          Many commands to be executed will need the names of files as
     arguments.  UNIX _p_a_t_h_n_a_m_e_s consist of a number of _c_o_m_p_o_n_e_n_t_s sep-
     arated by `/'.  Each component except the last names a  directory
     in  which  the  next  component resides, in effect specifying the
     _p_a_t_h of directories to follow to reach the file.  Thus the  path-
     name

          /etc/motd

     specifies  a  file in the directory `etc' which is a subdirectory
     of the _r_o_o_t directory `/'.  Within this directory the file  named
     is `motd' which stands for `message of the day'.  A _p_a_t_h_n_a_m_e that
     begins  with  a slash is said to be an _a_b_s_o_l_u_t_e pathname since it









     USD:4-8                            An Introduction to the C shell


     is specified from the absolute top of the entire directory  hier-
     archy  of  the  system  (the _r_o_o_t).  _P_a_t_h_n_a_m_e_s which do not begin
     with `/' are interpreted as starting in the current  _w_o_r_k_i_n_g  _d_i_-
     _r_e_c_t_o_r_y,  which  is,  by  default, your _h_o_m_e directory and can be
     changed dynamically by the _c_d  change  directory  command.   Such
     pathnames  are said to be _r_e_l_a_t_i_v_e to the working directory since
     they are found by starting in the working directory and  descend-
     ing  to  lower  levels  of  directories for each _c_o_m_p_o_n_e_n_t of the
     pathname.  If the pathname contains no slashes at  all  then  the
     file  is  contained in the working directory itself and the path-
     name is merely the name of the file in this directory.   Absolute
     pathnames have no relation to the working directory.

          Most  filenames  consist of a number of alphanumeric charac-
     ters and `.'s (periods).  In fact, all printing characters except
     `/' (slash) may appear in filenames.  It is inconvenient to  have
     most non-alphabetic characters in filenames because many of these
     have special meaning to the shell.  The character `.' (period) is
     not  a  shell-metacharacter and is often used to separate the _e_x_-
     _t_e_n_s_i_o_n of a file name from the base of the name.  Thus

          prog.c prog.o prog.errs prog.output

     are four related files.  They share a _b_a_s_e portion of a  name  (a
     base  portion  being  that  part  of the name that is left when a
     trailing `.' and following  characters  which  are  not  `.'  are
     stripped  off).   The  file  `prog.c' might be the source for a C
     program, the file `prog.o' the  corresponding  object  file,  the
     file  `prog.errs'  the errors resulting from a compilation of the
     program and the file `prog.output' the output of  a  run  of  the
     program.

          If  we  wished to refer to all four of these files in a com-
     mand, we could use the notation

          prog.*

     This expression is expanded by the shell, before the  command  to
     which  it  is an argument is executed, into a list of names which
     begin with `prog.'.  The character `*' here matches any  sequence
     (including the empty sequence) of characters in a file name.  The
     names which match are alphabetically sorted and placed in the _a_r_-
     _g_u_m_e_n_t _l_i_s_t of the command.  Thus the command

          echo prog.*

     will echo the names

          prog.c prog.errs prog.o prog.output

     Note that the names are in sorted order here, and a different or-
     der  than  we  listed them above.  The _e_c_h_o command receives four
     words as arguments, even though we only typed one word as an  ar-
     gument  directly.   The  four  words  were  generated by _f_i_l_e_n_a_m_e









     An Introduction to the C shell                            USD:4-9


     _e_x_p_a_n_s_i_o_n of the one input word.

          Other notations for _f_i_l_e_n_a_m_e _e_x_p_a_n_s_i_o_n are  also  available.
     The  character  `?'  matches  any single character in a filename.
     Thus

          echo ? ?? ???

     will echo a line of filenames; first  those  with  one  character
     names,  then  those  with  two character names, and finally those
     with three character names.  The names of each length will be in-
     dependently sorted.

          Another mechanism consists of a sequence of  characters  be-
     tween  `[' and `]'.  This metasequence matches any single charac-
     ter from the enclosed set.  Thus

          prog.[co]

     will match

          prog.c prog.o

     in the example above.  We can also place two characters around  a
     `-' in this notation to denote a range.  Thus

          chap.[1-5]

     might match files

          chap.1 chap.2 chap.3 chap.4 chap.5

     if they existed.  This is shorthand for

          chap.[12345]

     and otherwise equivalent.

          An  important  point  to  note is that if a list of argument
     words to a command (an _a_r_g_u_m_e_n_t _l_i_s_t_) contains filename expansion
     syntax, and if this filename expansion syntax fails to match  any
     existing file names, then the shell considers this to be an error
     and prints a diagnostic

          No match.

     and does not execute the command.

          Another  very important point is that files with the charac-
     ter `.' at the beginning are treated specially.  Neither `*'  nor
     `?' nor the `[' `]' mechanism will match it.  This prevents acci-
     dental matching of the filenames `.' and `..'  in the working di-
     rectory  which  have  special  meaning  to the system, as well as
     other files such as _._c_s_h_r_c which are not  normally  visible.   We









     USD:4-10                           An Introduction to the C shell


     will discuss the special role of the file _._c_s_h_r_c later.

          Another  filename  expansion  mechanism  gives access to the
     pathname of the _h_o_m_e directory of  other  users.   This  notation
     consists  of the character `~' (tilde) followed by another user's
     login name.  For instance the word `~bill' would map to the path-
     name  `/home/bill'  if  the  home  directory   for   `bill'   was
     `/home/bill'.   Since, on large systems, users may have login di-
     rectories scattered over many different disk volumes with differ-
     ent prefix directory names, this notation provides  a  convenient
     way of accessing the files of other users.

          A  special  case  of  this notation consists of a `~' alone,
     e.g. `~/mbox'.  This notation is expanded by the shell  into  the
     file  `mbox'  in _y_o_u_r home directory, i.e. into `/home/bill/mbox'
     for me on Ernie Co-vax, the UCB Computer Science  Department  VAX
     machine, where this document was prepared.  This can be very use-
     ful  if  you have used _c_d to change to another directory and have
     found a file you wish to copy using _c_p.  If I give the command

          cp thatfile ~

     the shell will expand this command to

          cp thatfile /home/bill

     since my home directory is /home/bill.

          There also exists a mechanism using the characters  `{'  and
     `}'  for  abbreviating a set of words which have common parts but
     cannot be abbreviated by the above mechanisms  because  they  are
     not files, are the names of files which do not yet exist, are not
     thus  conveniently  described.   This mechanism will be described
     much later, in section 4.2, as it is used less frequently.

     11..77..  QQuuoottaattiioonn

          We have already seen a number of metacharacters used by  the
     shell.  These metacharacters pose a problem in that we cannot use
     them directly as parts of words.  Thus the command

          echo *

     will  not  echo  the character `*'.  It will either echo a sorted
     list of filenames in the current _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y, or print  the
     message  `No  match'  if there are no files in the working direc-
     tory.

          The recommended mechanism for placing characters  which  are
     neither numbers, digits, `/', `.', nor `-' in an argument word to
     a command, is to enclose it with single quotation characters `'',












     An Introduction to the C shell                           USD:4-11


     i.e.

          echo '*'

     There  is  one special character `!' which is used by the _h_i_s_t_o_r_y
     mechanism of the shell and which cannot be _e_s_c_a_p_e_d by placing  it
     within  `''  characters.   It and the character `'' itself can be
     preceded by a single `\' to prevent their special meaning.  Thus

          echo \'\!

     prints

          '!

     These two mechanisms suffice to place any printing character into
     a word which is an argument to a shell command.  They can be com-
     bined, as in

          echo \''*'

     which prints

          '*

     since the first `\' escaped the first `'' and  the  `*'  was  en-
     closed between `'' characters.

     11..88..  TTeerrmmiinnaattiinngg ccoommmmaannddss

          When  you  are  executing a command and the shell is waiting
     for it to complete, there are several ways to force it  to  stop.
     For instance if you type the command

          cat /etc/passwd

     the system will print a copy of a list of all users of the system
     on your terminal.  This is likely to continue for several minutes
     unless  you stop it.  You can send an INTERRUPT _s_i_g_n_a_l to the _c_a_t
     command  by typing ^C on your terminal.*  Since _c_a_t does not take
     any precautions to avoid or otherwise handle this signal, the IN-
     TERRUPT will cause it to terminate.  The shell notices  that  _c_a_t
     has  terminated  and prompts you again with `% '.  If you hit IN-
     TERRUPT again, the shell will just repeat  its  prompt  since  it
     handles INTERRUPT signals and chooses to continue to execute com-
     mands  rather than terminating like _c_a_t did, which would have the
     effect of logging you out.

          Another way in which many programs terminate  is  when  they
     get an end-of-file from their standard input.  Thus the _m_a_i_l pro-
     gram in the first example above was terminated when we typed a ^D
     -----------
     *On some older Unix systems the DEL or RUBOUT  key  has  the
     same effect. "stty all" will tell you the `intr' key value.









     USD:4-12                           An Introduction to the C shell


     which  generates  an  end-of-file  from  the standard input.  The
     shell also terminates when it gets an end-of-file, printing  `lo-
     gout';  UNIX then logs you off the system.  Since this means that
     typing too many ^D's can accidentally log us off, the shell has a
     mechanism for preventing this.  This  _i_g_n_o_r_e_e_o_f  option  will  be
     discussed in section 2.2.

          If  a command has its standard input redirected from a file,
     then it will normally terminate when it reaches the end  of  this
     file.  Thus if we execute

          mail bill < prepared.text

     the mail command will terminate without our typing a ^D.  This is
     because it read to the end-of-file of our file `prepared.text' in
     which  we placed a message for `bill' with an editor program.  We
     could also have done

          cat prepared.text | mail bill

     since the _c_a_t command would then have written  the  text  through
     the pipe to the standard input of the mail command.  When the _c_a_t
     command  completed  it  would  have  terminated, closing down the
     pipeline and the _m_a_i_l command would have received an  end-of-file
     from  it  and  terminated.  Using a pipe here is more complicated
     than redirecting input so we would  more  likely  use  the  first
     form.   These commands could also have been stopped by sending an
     INTERRUPT.

          Another possibility for stopping a command is to suspend its
     execution temporarily, with the possibility of continuing  execu-
     tion  later.   This is done by sending a STOP signal via typing a
     ^Z.  This signal causes all  commands  running  on  the  terminal
     (usually one, but more if a pipeline is executing) to become sus-
     pended.   The  shell  notices  that the command(s) have been sus-
     pended, types `Suspended', and then prompts for  a  new  command.
     The  previously  executing command has been suspended, but other-
     wise unaffected by the STOP signal.  Any other  commands  can  be
     executed  while the original command remains suspended.  The sus-
     pended command can be continued using the _f_g command with no  ar-
     guments.   The  shell  will then retype the command to remind you
     which command is being continued, and cause the command to resume
     execution.  Unless any input files in use by the  suspended  com-
     mand have been changed in the meantime, the suspension has no ef-
     fect  whatsoever  on  the execution of the command.  This feature
     can be very useful during editing, when you need to look  at  an-
     other  file  before  continuing. An example of command suspension
















     An Introduction to the C shell                           USD:4-13


     follows.

          % mail harold
          Someone just copied a big file into my directory and its name is
          ^Z
          Suspended
          % ls
          funnyfile
          prog.c
          prog.o
          % jobs
          [1]  + Suspended mail harold
          % fg
          mail harold
          (continue)
          funnyfile. Do you know who did it?
          EOT
          %

     In this example someone was sending a message to Harold and  for-
     got  the name of the file he wanted to mention.  The mail command
     was suspended by typing ^Z.  When the shell noticed that the mail
     program was suspended, it typed `Suspended' and  prompted  for  a
     new  command.  Then the _l_s command was typed to find out the name
     of the file.  The _j_o_b_s command was run to find out which  command
     was  suspended. At this time the _f_g command was typed to continue
     execution of the mail program.  Input to  the  mail  program  was
     then continued and ended with a ^D which indicated the end of the
     message  at which time the mail program typed EOT.  The _j_o_b_s com-
     mand will show which commands are suspended.  The ^Z should  only
     be typed at the beginning of a line since everything typed on the
     current  line  is  discarded  when a signal is sent from the key-
     board.  This also happens on INTERRUPT, and QUIT  signals.   More
     information  on  suspending jobs and controlling them is given in
     section 2.6.

          If you write or run programs which are not  fully  debugged,
     then  it  may  be  necessary  to stop them somewhat ungracefully.
     This can be done by sending them a QUIT signal, sent by typing  a
     ^\.   This  will  usually  provoke the shell to produce a message
     like:

          Quit (core dumped)

     indicating that a file `core' has been created containing  infor-
     mation  about  the running program's state when it terminated due
     to the QUIT signal.  You can examine this file yourself, or  for-
     ward information to the maintainer of the program telling him/her
     where the _c_o_r_e _f_i_l_e is.

          If you run background commands (as explained in section 2.6)
     then these commands will ignore INTERRUPT and QUIT signals at the
     terminal.   To stop them you must use the _k_i_l_l command.  See sec-
     tion 2.6 for an example.









     USD:4-14                           An Introduction to the C shell


          If you want to examine the output of a command without  hav-
     ing it move off the screen as the output of the

          cat /etc/passwd

     command will, you can use the command

          more /etc/passwd

     The _m_o_r_e program pauses after each complete screen-full and types
     `[filename] %', at which point you can hit a space to get another
     screen  full,  a  return  to get another line, an `h' to get some
     help on other commands, or a `q' to end the  _m_o_r_e  program.   You
     can also use more as a filter, i.e.

          cat /etc/passwd | more

     works just like the more simple more command above.

          For  stopping output of commands not involving _m_o_r_e, you can
     use the ^S key to stop the typeout.  The typeout will resume when
     you hit ^Q or any other key, but ^Q is normally used  because  it
     only restarts the output and does not become input to the program
     which is running.  This works well on low-speed terminals, but at
     9600  baud  it  is hard to type ^S and ^Q fast enough to paginate
     the output nicely, and a program like _m_o_r_e is usually used.

          An additional possibility is to  use  the  ^O  flush  output
     character; when this character is typed, all output from the cur-
     rent  command  is thrown away (quickly) until the next input read
     occurs or until the next shell prompt.  This can be used to allow
     a command to complete without having to suffer through the output
     on a slow terminal; ^O is a toggle, so flushing can be turned off
     by typing ^O again while output is being flushed.

     11..99..  WWhhaatt nnooww??

          We have so far seen a number of mechanisms of the shell  and
     learned  a lot about the way in which it operates.  The remaining
     sections will go yet further into the internals of the shell, but
     you will surely want to try using the shell  before  you  go  any
     further.  To try it you can log in to UNIX and type the following
     command to the system:

          chsh -s /bin/csh myname

     Here  `myname'  should  be  replaced by the name you typed to the
     system prompt of `login:' to get onto the system.  Thus  I  would
     use  `chsh  -s /bin/csh bill'.  YYoouu oonnllyy hhaavvee ttoo ddoo tthhiiss oonnccee;; iitt
     ttaakkeess eeffffeecctt aatt nneexxtt llooggiinn..  You are now ready to try using _c_s_h.

          Before you do the `chsh' command, the shell  you  are  using
     when  you log into the system is `/bin/sh'.  In fact, much of the
     above discussion is applicable to `/bin/sh'.   The  next  section









     An Introduction to the C shell                           USD:4-15


     will  introduce  many  features  particular  to _c_s_h so you should
     change your shell to _c_s_h before you begin reading it.





























































     USD:4-16                           An Introduction to the C shell


     22..  DDeettaaiillss oonn tthhee sshheellll ffoorr tteerrmmiinnaall uusseerrss

     22..11..  SShheellll ssttaarrttuupp aanndd tteerrmmiinnaattiioonn

          When you login, the shell is started by the system  in  your
     _h_o_m_e  directory and begins by reading commands from a file _._c_s_h_r_c
     in this directory.  All shells which you may  start  during  your
     terminal  session  will  read  from this file.  We will later see
     what kinds of commands are usefully placed  there.   For  now  we
     need not have this file and the shell does not complain about its
     absence.

          A _l_o_g_i_n _s_h_e_l_l, executed after you login to the system, will,
     after  it  reads  commands from _._c_s_h_r_c, read commands from a file
     _._l_o_g_i_n also in your home directory.  This file contains  commands
     which you wish to do each time you log in to the UNIX system.  My
     _._l_o_g_i_n file looks something like:

          set ignoreeof
          set mail=(/usr/spool/mail/bill)
          echo "${prompt}users" ; users
          alias ts \
               'set noglob ; eval `tset -s -m dialup:c100rv4pna -m plugboard:?hp2621nl *`';
          ts; stty intr ^C kill ^U crt
          set time=15 history=10
          if (-e $mail) then
               echo "${prompt}mail"
               mail
          endif

          This  file  contains several commands to be executed by UNIX
     each time I log in.  The first is a _s_e_t command which  is  inter-
     preted  directly  by  the  shell.  It sets the shell variable _i_g_-
     _n_o_r_e_e_o_f which causes the shell to not log me off  if  I  hit  ^D.
     Rather,  I use the _l_o_g_o_u_t command to log off the system.  By set-
     ting the _m_a_i_l variable, I ask the shell  to  watch  for  incoming
     mail  to  me.   Every 5 minutes the shell looks for this file and
     tells me if more mail has arrived there.  An alternative to  this
     is to put the command

          biff y

     in  place  of this _s_e_t; this will cause me to be notified immedi-
     ately when mail arrives, and to be shown the first few  lines  of
     the new message.

          I  create  an  _a_l_i_a_s ``ts'' which executes a tset(1) command
     setting up the modes of the terminal.  The parameters to _t_s_e_t in-
     dicate the kinds of terminal which I usually use when  not  on  a
     hardwired port.  I then execute ``ts'' and also use the _s_t_t_y com-
     mand  to  change  the interrupt character to ^C and the line kill
     character to ^U.











     An Introduction to the C shell                           USD:4-17


          Next I set the shell variable `time'  to  `15'  causing  the
     shell  to  automatically  print out statistics lines for commands
     which execute for at least 15 seconds of CPU time.  The  variable
     `history' is set to 10 indicating that I want the shell to remem-
     ber  the  last 10 commands I type in its _h_i_s_t_o_r_y _l_i_s_t, (described
     later).

          Finally, if my mailbox file exists, then I  run  the  `mail'
     program to process my mail.

          When the `mail' program finishes, the shell will finish pro-
     cessing my _._l_o_g_i_n file and begin reading commands from the termi-
     nal, prompting for each with `% '.  When I log off (by giving the
     _l_o_g_o_u_t  command)  the  shell will print `logout' and execute com-
     mands from the file `.logout' if it exists in my home  directory.
     After  that the shell will terminate and UNIX will log me off the
     system.  If the system is not going down, I will  receive  a  new
     login message.  In any case, after the `logout' message the shell
     is  committed  to terminating and will take no further input from
     my terminal.

     22..22..  SShheellll vvaarriiaabblleess

          The shell maintains a set of _v_a_r_i_a_b_l_e_s.  We  saw  above  the
     variables  _h_i_s_t_o_r_y  and  _t_i_m_e which had values `10' and `15'.  In
     fact, each shell variable has as value an array of zero  or  more
     _s_t_r_i_n_g_s.   Shell variables may be assigned values by the set com-
     mand.  It has several forms, the most useful of which  was  given
     above and is

          set name=value

          Shell  variables may be used to store values which are to be
     used in commands later through  a  substitution  mechanism.   The
     shell  variables  most  commonly  referenced  are, however, those
     which the shell itself refers to.   By  changing  the  values  of
     these  variables  one  can  directly  affect  the behavior of the
     shell.

          One of the most important variables is  the  variable  _p_a_t_h.
     This  variable  contains  a sequence of directory names where the
     shell searches for commands.  The _s_e_t command with  no  arguments
     shows  the  value  of all variables currently defined (we usually
     say _s_e_t) in the shell.  The default value for path will be  shown



















     USD:4-18                           An Introduction to the C shell


     by _s_e_t to be

          % set
          argv   ()
          cwd    /home/bill
          home   /home/bill
          path   (/bin /usr/bin .)
          prompt %
          shell  /bin/csh
          status 0
          term   c100rv4pna
          user   bill
          %

     This  output  indicates  that the variable path points to `/bin',
     `/usr/bin', and the current directory `.'.   Commands  which  you
     may  write  might  be  in  `.' (usually one of your directories).
     Other commands may live in /bin and /usr/bin.

          A number of locally developed programs on the system live in
     the directory `/usr/local/bin'.  If we wish that all shells which
     we invoke to have access to these new programs we can  place  the
     command

          set path=(/bin /usr/bin /usr/local/bin .)

     in  our  file  _._c_s_h_r_c  in our home directory.  Try doing this and
     then logging out and back in and do

          set

     again to see that the value assigned to _p_a_t_h has changed.

          One thing you should be aware of is that the shell  examines
     each  directory  which  you  insert into your path and determines
     which commands are contained there.  Except for the  current  di-
     rectory `.', which the shell treats specially, this means that if
     commands  are  added to a directory in your search path after you
     have started the shell, they will not necessarily be found by the
     shell.  If you wish to use a command which has been added in this
     way, you should give the command

          rehash

     to the shell, which will cause it to recompute its internal table
     of command locations, so that it will find the newly  added  com-
     mand.   Since  the shell has to look in the current directory `.'
     on each command, placing it at the end of the path  specification
     usually works equivalently and reduces overhead.

          Other useful built in variables are the variable _h_o_m_e, which
     shows your home directory; _c_w_d, which contains your current work-
     ing  directory;  and  the variable _i_g_n_o_r_e_e_o_f, which can be set in
     your _._l_o_g_i_n file to tell the shell not to exit when  it  receives









     An Introduction to the C shell                           USD:4-19


     an  end-of-file  from a terminal (as described above).  The vari-
     able `ignoreeof' is one of several variables which the shell does
     not care about the value of, only whether they are _s_e_t or  _u_n_s_e_t.
     Thus to set this variable you simply do

          set ignoreeof

     and to unset it do

          unset ignoreeof

     These give the variable `ignoreeof' no value, but none is desired
     or required.

          Finally,  some other built-in shell variables of use are the
     variables _n_o_c_l_o_b_b_e_r and _m_a_i_l.  The metasyntax

          > filename

     which redirects the standard output of a command, will  overwrite
     and destroy the previous contents of the named file.  In this way
     you  may accidentally overwrite a file which is valuable.  If you
     would prefer that the shell not overwrite files in this  way  you
     can

          set noclobber

     in your _._l_o_g_i_n file.  Then trying to do

          date > now

     would  cause  a  diagnostic  if `now' existed already.  You could
     type

          date >!  now

     if you really wanted to overwrite the  contents  of  `now'.   The
     `>!'  is a special metasyntax indicating that clobbering the file
     is ok.*

     22..33..  TThhee sshheellll''ss hhiissttoorryy lliisstt

          The  shell  can maintain a _h_i_s_t_o_r_y _l_i_s_t into which it places
     the words of previous commands.  It is possible to use a notation
     to reuse commands or words from commands in forming new commands.
     This mechanism can be used to repeat previous commands or to cor-
     rect minor typing mistakes in commands.

          The following figure gives a sample session involving  typi-
     cal usage of the history mechanism of the shell.  In this example
     -----------
     *The space between the `!' and the word  `now'  is  critical
     here, as `!now' would be an invocation of the _h_i_s_t_o_r_y mecha-
     nism, and have a totally different effect.









     USD:4-20                           An Introduction to the C shell



          % cat bug.c
          main()

          {
               printf("hello);
          }
          % cc !$
          cc bug.c
          bug.c:4: unterminated string or character constant
          bug.c:4: possible real start of unterminated constant
          % ed !$
          ed bug.c
          29
          4s/);/"&/p
                  printf("hello");
          w
          30
          q
          % !c
          cc bug.c
          % a.out
          hello% !e
          ed bug.c
          30
          4s/lo/lo\\n/p
                  printf("hello\n");
          w
          32
          q
          % !c -o bug
          cc bug.c -o bug
          % size a.out bug
          text data bss  dec  hex
          1022 208  288  1518 5ee  a.out
          1022 208  288  1518 5ee  bug
          % ls -l !*
          ls -l a.out bug
          -rwxr-xr-x 1 bill       3932 Dec 19 09:41 a.out
          -rwxr-xr-x 1 bill       3932 Dec 19 09:42 bug
          % bug
          hello
          % cay -n bug.c
          cay: Command not found.
          % ^cay^cat
          cat -n bug.c
              1     main()
              3     {
              4          printf("hello\n");
              5     }
          % !! | lpr
          cat -n bug.c | lpr
          %










     An Introduction to the C shell                           USD:4-21


     we have a very simple C program which has a bug (or two) in it in
     the  file  `bug.c',  which we `cat' out on our terminal.  We then
     try to run the C compiler on it, referring to the file  again  as
     `!$',  meaning  the  last argument to the previous command.  Here
     the `!' is the history mechanism  invocation  metacharacter,  and
     the  `$'  stands  for the last argument, by analogy to `$' in the
     editor which stands for the end of the line.   The  shell  echoed
     the  command, as it would have been typed without use of the his-
     tory mechanism, and then executed it.   The  compilation  yielded
     error  diagnostics  so  we now run the editor on the file we were
     trying to compile, fix the bug, and run  the  C  compiler  again,
     this time referring to this command simply as `!c', which repeats
     the  last  command  which  started with the letter `c'.  If there
     were other commands starting with `c' done recently we could have
     said `!cc' or even `!cc:p' which would have printed the last com-
     mand starting with `cc' without executing it.

          After this recompilation, we ran the resulting `a.out' file,
     and then noting that there still was a bug, ran the editor again.
     After fixing the program we ran the C compiler again, but  tacked
     onto  the command an extra `-o bug' telling the compiler to place
     the resultant binary in the file `bug' rather than  `a.out'.   In
     general,  the history mechanisms may be used anywhere in the for-
     mation of new commands and other characters may be placed  before
     and after the substituted commands.

          We  then  ran the `size' command to see how large the binary
     program images we have created were, and then an `ls -l'  command
     with the same argument list, denoting the argument list `*'.  Fi-
     nally  we  ran the program `bug' to see that its output is indeed
     correct.

          To make a numbered listing of the program we ran  the  `cat'
     command  on the file `bug.c', with the `-n' flag.  Unfortunately,
     we mispelled `cat' as `cay'.  To correct this  we  used  a  shell
     substitute, placing the old text and new text between `^' charac-
     ters.   This  is similar to the substitute command in the editor.
     Finally, we repeated the same command with  `!!',  but  sent  its
     output to the line printer.

          There are other mechanisms available for repeating commands.
     The _h_i_s_t_o_r_y command prints out a number of previous commands with
     numbers by which they can be referenced.  There is a way to refer
     to a previous command by searching for a string which appeared in
     it, and there are other, less useful, ways to select arguments to
     include  in  a  new command.  A complete description of all these
     mechanisms is given in the C shell manual pages in the UNIX  Pro-
     grammer's Manual.

     22..44..  AAlliiaasseess

          The  shell  has an _a_l_i_a_s mechanism which can be used to make
     transformations on input commands.  This mechanism can be used to
     simplify the commands you type, to supply  default  arguments  to









     USD:4-22                           An Introduction to the C shell


     commands, or to perform transformations on commands and their ar-
     guments.   The  alias  facility  is  similar to a macro facility.
     Some of the features obtained by aliasing can  be  obtained  also
     using  shell  command  files, but these take place in another in-
     stance of the shell and cannot directly affect the current shells
     environment or involve commands such as _c_d which must be done  in
     the current shell.

          As  an  example,  suppose that there is a new version of the
     mail program on the system called  `newmail'  you  wish  to  use,
     rather than the standard mail program which is called `mail'.  If
     you place the shell command

          alias mail newmail

     in  your  _._c_s_h_r_c  file, the shell will transform an input line of
     the form

          mail bill

     into a call on `newmail'.  More generally, suppose  we  wish  the
     command `ls' to always show sizes of files, that is, to always do
     `-s'.  We can do

          alias ls ls -s

     or even

          alias dir ls -s

     creating a new command syntax `dir' which does an `ls -s'.  If we
     say

          dir ~bill

     then the shell will translate this to

          ls -s /home/bill

          Thus  the _a_l_i_a_s mechanism can be used to provide short names
     for commands, to provide default arguments,  and  to  define  new
     short  commands  in terms of other commands.  It is also possible
     to define aliases which contain multiple commands  or  pipelines,
     showing  where  the  arguments  to the original command are to be
     substituted using the facilities of the history mechanism.   Thus
     the definition

          alias cd 'cd \!* ; ls '

     would  do  an  _l_s command after each change directory _c_d command.
     We enclosed the entire alias definition in `'' characters to pre-
     vent most substitutions from occurring and the character `;' from
     being recognized as a metacharacter.  The  `!'  here  is  escaped
     with  a  `\'  to prevent it from being interpreted when the alias









     An Introduction to the C shell                           USD:4-23


     command is typed in.  The `\!*' here substitutes the entire argu-
     ment list to the pre-aliasing _c_d command, without giving an error
     if there were no arguments.  The `;' separating commands is  used
     here  to  indicate  that  one  command is to be done and then the
     next.  Similarly the definition

          alias whois 'grep \!^ /etc/passwd'

     defines a command which looks up its first argument in the  pass-
     word file.

          WWaarrnniinngg:: The shell currently reads the _._c_s_h_r_c file each time
     it  starts  up.   If  you place a large number of commands there,
     shells will tend to start slowly.  A  mechanism  for  saving  the
     shell  environment  after  reading  the  _._c_s_h_r_c  file and quickly
     restoring it is under development, but for now you should try  to
     limit the number of aliases you have to a reasonable number... 10
     or  15  is  reasonable, 50 or 60 will cause a noticeable delay in
     starting up shells, and make the system seem  sluggish  when  you
     execute commands from within the editor and other programs.

     22..55..  MMoorree rreeddiirreeccttiioonn;; >>>> aanndd >>&&

          There  are  a few more notations useful to the terminal user
     which have not been introduced yet.

          In addition to the standard output, commands also have a _d_i_-
     _a_g_n_o_s_t_i_c _o_u_t_p_u_t which is normally directed to the  terminal  even
     when  the  standard output is redirected to a file or a pipe.  It
     is occasionally desirable to direct the diagnostic  output  along
     with  the  standard output.  For instance if you want to redirect
     the output of a long running command into a file and wish to have
     a record of any error diagnostic it produces you can do

          command >& file

     The `>&' here tells the shell to route both the diagnostic output
     and the standard output into `file'.  Similarly you can give  the
     command

          command |& lpr

     to  route both standard and diagnostic output through the pipe to
     the line printer daemon _l_p_r.**

          Finally, it is possible to use the form

          command >> file

     -----------
     ** A command of the form
          command >&! file
     exists, and is used when _n_o_c_l_o_b_b_e_r is set and  _f_i_l_e  already
     exists.









     USD:4-24                           An Introduction to the C shell


     to place output at the end of an existing file.*

     22..66..  JJoobbss;; BBaacckkggrroouunndd,, FFoorreeggrroouunndd,, oorr SSuussppeennddeedd

          When one or more commands are typed together as  a  pipeline
     or  as  a  sequence of commands separated by semicolons, a single
     _j_o_b is created by the shell consisting of these commands together
     as a unit.  Single commands without pipes  or  semicolons  create
     the  simplest  jobs.  Usually, every line typed to the shell cre-
     ates a job.  Some lines that create jobs (one per line) are

          sort < data
          ls -s | sort -n | head -5
          mail harold

          If the metacharacter `&' is typed at the  end  of  the  com-
     mands,  then  the job is started as a _b_a_c_k_g_r_o_u_n_d job.  This means
     that the shell does not wait for it to complete  but  immediately
     prompts  and  is  ready for another command.  The job runs _i_n _t_h_e
     _b_a_c_k_g_r_o_u_n_d at the same time that normal jobs,  called  _f_o_r_e_g_r_o_u_n_d
     jobs,  continue  to  be  read  and executed by the shell one at a
     time.  Thus

          du > usage &

     would run the _d_u program, which reports on the disk usage of your
     working directory (as well as any directories below it), put  the
     output into the file `usage' and return immediately with a prompt
     for  the  next  command without waiting for _d_u to finish.  The _d_u
     program would continue executing in the background until it  fin-
     ished,  even though you can type and execute more commands in the
     meantime.  When a background job terminates, a message  is  typed
     by the shell just before the next prompt telling you that the job
     has  completed.   In  the  following  example the _d_u job finishes
     sometime during the execution of the _m_a_i_l command and its comple-
     tion is reported just before the prompt after  the  _m_a_i_l  job  is
     finished.

          % du > usage &
          [1] 503
          % mail bill
          How do you know when a background job is finished?
          EOT
          [1] - Done       du > usage
          %

     If  the  job did not terminate normally, the `Done' message might
     -----------
     *  If  _n_o_c_l_o_b_b_e_r  is  set, then an error will result if _f_i_l_e
     does not exist, otherwise the shell will create _f_i_l_e  if  it
     doesn't exist.  A form
          command >>! file
     makes  it not be an error for file to not exist when _n_o_c_l_o_b_-
     _b_e_r is set.









     An Introduction to the C shell                           USD:4-25


     say something else like `Killed'.  If you want  the  terminations
     of  background jobs to be reported at the time they occur (possi-
     bly interrupting the output of other foreground  jobs),  you  can
     set the _n_o_t_i_f_y variable.  In the previous example this would mean
     that  the  `Done'  message might have come right in the middle of
     the message to Bill.  Background jobs are unaffected by any  sig-
     nals  from the keyboard like the STOP, INTERRUPT, or QUIT signals
     mentioned earlier.

          Jobs are recorded in a table inside  the  shell  until  they
     terminate.  In this table, the shell remembers the command names,
     arguments, and the _p_r_o_c_e_s_s _n_u_m_b_e_r_s of all commands in the job, as
     well  as  the  working directory where the job was started.  Each
     job in the table is either running _i_n  _t_h_e  _f_o_r_e_g_r_o_u_n_d  with  the
     shell  waiting for it to terminate, running _i_n _t_h_e _b_a_c_k_g_r_o_u_n_d, or
     _s_u_s_p_e_n_d_e_d.  Only one job can be running in the foreground at  one
     time,  but  several jobs can be suspended or running in the back-
     ground at once.  As each job is started, it is assigned  a  small
     identifying  number called the _j_o_b _n_u_m_b_e_r which can be used later
     to refer to the job in the commands described below.  Job numbers
     remain the same until the job terminates and then are re-used.

          When a job is started in the background using `&', its  num-
     ber,  as  well as the process numbers of all its (top level) com-
     mands, is typed by the shell before  prompting  you  for  another
     command. For example,

          % ls -s | sort -n > usage &
          [2] 2034 2035
          %

     runs  the  `ls'  program  with the `-s' option, pipes this output
     into the `sort' program with the `-n' option, which puts its out-
     put into the file `usage'.  Since the `&' was at the end  of  the
     line,  these  two  programs were started together as a background
     job.  After starting the job, the shell prints the job number  in
     brackets  (2 in this case) followed by the process number of each
     program started in the job.  Then the shell  immediately  prompts
     for a new command, leaving the job running simultaneously.

          As  mentioned  in  section  1.8, foreground jobs become _s_u_s_-
     _p_e_n_d_e_d by typing ^Z, which sends a STOP signal to  the  currently
     running foreground job.  A background job can become suspended by
     using  the _s_t_o_p command described below.  When jobs are suspended
     they merely stop any further progress until started again, either
     in the foreground or the background.  The shell  notices  when  a
     job  becomes  stopped and reports this fact, much like it reports
     the termination of background jobs.   For  foreground  jobs  this















     USD:4-26                           An Introduction to the C shell


     looks like

          % du > usage
          ^Z
          Suspended
          %

     The  `Suspended'  message  is  typed by the shell when it notices
     that the _d_u program stopped.  For background jobs, using the _s_t_o_p
     command, it is

          % sort usage &
          [1] 2345
          % stop %1
          [1] + Suspended (signal)sort usage
          %

     Suspending foreground jobs can be very useful when  you  need  to
     temporarily  change  what  you are doing (execute other commands)
     and then return to the suspended job.  Also, foreground jobs  can
     be  suspended  and then continued as background jobs using the _b_g
     command, allowing you to continue other work and stop waiting for
     the foreground job to finish.  Thus

          % du > usage
          ^Z
          Stopped
          % bg
          [1] du > usage &
          %

     starts `du' in the foreground, stops it before it finishes,  then
     continues  it in the background allowing more foreground commands
     to be executed.  This is especially helpful when a foreground job
     ends up taking longer than you expected  and  you  wish  you  had
     started it in the background in the beginning.

          All  _j_o_b  _c_o_n_t_r_o_l commands can take an argument that identi-
     fies a particular job.  All job name  arguments  begin  with  the
     character `%', since some of the job control commands also accept
     process  numbers  (printed  by  the _p_s command).  The default job
     (when no argument is given) is called  the  _c_u_r_r_e_n_t  job  and  is
     identified  by  a  `+'  in  the output of the _j_o_b_s command, which
     shows you which jobs you have.  When only one job is  stopped  or
     running  in the background (the usual case) it is always the cur-
     rent job; thus no argument is needed.  If a job is stopped  while
     running  in the foreground it becomes the _c_u_r_r_e_n_t job and the ex-
     isting current job becomes the _p_r_e_v_i_o_u_s job - identified by a `-'
     in the output of _j_o_b_s.  When the current job terminates, the pre-
     vious job becomes the current job.  When given, the  argument  is
     either  `%-'  (indicating the previous job); `%#', where # is the
     job number; `%pref' where pref is some unique prefix of the  com-
     mand  name  and arguments of one of the jobs; or `%?' followed by
     some string found in only one of the jobs.









     An Introduction to the C shell                           USD:4-27


          The _j_o_b_s command types the table of  jobs,  giving  the  job
     number,  commands  and  status (`Suspended' or `Running') of each
     background or suspended job.  With the `-l'  option  the  process
     numbers are also typed.

          % du > usage &
          [1] 3398
          % ls -s | sort -n > myfile &
          [2] 3405
          % mail bill
          ^Z
          Stopped
          % jobs
          [1] - Running    du > usage
          [2]    Running   ls -s | sort -n > myfile
          [3] + Suspended  mail bill
          % fg %ls
          ls -s | sort -n > myfile
          % more myfile

          The  _f_g  command  runs  a suspended or background job in the
     foreground.  It is used to restart a previously suspended job  or
     change  a  background job to run in the foreground (allowing sig-
     nals or input from the terminal).  In the above example  we  used
     _f_g  to  change the `ls' job from the background to the foreground
     since we wanted to wait for it to finish before  looking  at  its
     output  file.   The  _b_g command runs a suspended job in the back-
     ground.  It is usually used after stopping the currently  running
     foreground job with the STOP signal.  The combination of the STOP
     signal  and  the _b_g command changes a foreground job into a back-
     ground job.  The _s_t_o_p command suspends a background job.

          The _k_i_l_l command terminates a background  or  suspended  job
     immediately.   In  addition to jobs, it may be given process num-
     bers as arguments, as printed by _p_s_.  Thus, in the example above,
     the running _d_u command could have been terminated by the command

          % kill %1
          [1]  Terminated  du > usage
          %

          The _n_o_t_i_f_y command (not the variable mentioned earlier)  in-
     dicates that the termination of a specific job should be reported
     at the time it finishes instead of waiting for the next prompt.

          If  a job running in the background tries to read input from
     the terminal it is automatically stopped.  When  such  a  job  is
     then  run  in  the foreground, input can be given to the job.  If
     desired, the job can be run in the background again until it  re-
     quests  input  again.   This  is illustrated in the following se-
     quence where the `s' command in the text editor might take a long
     time.

          % ed bigfile









     USD:4-28                           An Introduction to the C shell


          120000
          1,$s/thisword/thatword/
          ^Z
          Suspended
          % bg
          [1] ed bigfile &
          %
           . . .  some foreground commands
          [1] Suspended (tty input)ed bigfile
          % fg
          ed bigfile
          w
          120000
          q
          %

     So after the `s' command was issued, the  `ed'  job  was  stopped
     with ^Z and then put in the background using _b_g.  Some time later
     when  the `s' command was finished, _e_d tried to read another com-
     mand and was stopped because jobs in the background  cannot  read
     from  the  terminal.  The _f_g command returned the `ed' job to the
     foreground where it could once again  accept  commands  from  the
     terminal.

          The command

          stty tostop

     causes all background jobs run on your terminal to stop when they
     are  about  to  write output to the terminal.  This prevents mes-
     sages from background jobs from interrupting foreground job  out-
     put  and allows you to run a job in the background without losing
     terminal output.  It also can be used  for  interactive  programs
     that  sometimes have long periods without interaction.  Thus each
     time it outputs a prompt for more input it will stop  before  the
     prompt.   It can then be run in the foreground using _f_g, more in-
     put can be given and, if necessary stopped and  returned  to  the
     background.   This  _s_t_t_y  command might be a good thing to put in
     your _._l_o_g_i_n file if you do not like output from  background  jobs
     interrupting  your  work.   It also can reduce the need for redi-
     recting the output of background jobs if the output is  not  very






















     An Introduction to the C shell                           USD:4-29


     big:

          % stty tostop
          % wc hugefile &
          [1] 10387
          % ed text
          . . . some time later
          q
          [1] Stopped (tty output)wc hugefile
          % fg wc
          wc hugefile
             13371   30123   302577
          % stty -tostop

     Thus  after  some  time the `wc' command, which counts the lines,
     words and characters in a file, had one line of output.  When  it
     tried to write this to the terminal it stopped.  By restarting it
     in  the foreground we allowed it to write on the terminal exactly
     when we were ready to look at its output.  Programs which attempt
     to change the mode of the terminal will also  block,  whether  or
     not  _t_o_s_t_o_p  is  set,  when they are not in the foreground, as it
     would be very unpleasant to have  a  background  job  change  the
     state of the terminal.

          Since  the _j_o_b_s command only prints jobs started in the cur-
     rently executing shell, it knows nothing  about  background  jobs
     started  in  other  login sessions or within shell files.  The _p_s
     command can be used in this case to  find  out  about  background
     jobs not started in the current shell.

     22..77..  WWoorrkkiinngg DDiirreeccttoorriieess

          As  mentioned  in section 1.6, the shell is always in a par-
     ticular _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y.  The `change directory' command  _c_h_d_i_r
     (its  short  form _c_d may also be used) changes the working direc-
     tory of the shell, that is, changes the directory you are located
     in.

          It is useful to make a directory for each project  you  wish
     to work on and to place all files related to that project in that
     directory.   The  `make  directory' command, _m_k_d_i_r, creates a new
     directory.  The _p_w_d (`print working directory')  command  reports
     the absolute pathname of the working directory of the shell, that
     is, the directory you are located in.  Thus in the example below:

          % pwd
          /home/bill
          % mkdir newpaper
          % chdir newpaper
          % pwd
          /home/bill/newpaper
          %

     the  user has created and moved to the directory _n_e_w_p_a_p_e_r, where,









     USD:4-30                           An Introduction to the C shell


     for example, he might place a group of related files.

          No matter where you have moved to in a directory  hierarchy,
     you can return to your `home' login directory by doing just

          cd

     with  no  arguments.   The  name  `..' always means the directory
     above the current one in the hierarchy; thus

          cd ..

     changes the shell's working directory to the one  directly  above
     the  current  one.   The  name  `..' can be used in any pathname;
     thus,

          cd ../programs

     means change to the directory `programs' contained in the  direc-
     tory  above the current one.  If you have several directories for
     different projects under, say, your home directory,  this  short-
     hand notation permits you to switch easily between them.

          The shell always remembers the pathname of its current work-
     ing  directory  in  the  variable _c_w_d.  The shell can also be re-
     quested to remember the previous directory when you change  to  a
     new  working directory.  If the `push directory' command _p_u_s_h_d is
     used in place of the _c_d command, the shell saves the name of  the
     current working directory on a _d_i_r_e_c_t_o_r_y _s_t_a_c_k before changing to
     the  new  one.   You  can see this list at any time by typing the
     `directories' command _d_i_r_s.

          % pushd newpaper/references
          ~/newpaper/references  ~
          % pushd /usr/lib/tmac
          /usr/lib/tmac  ~/newpaper/references  ~
          % dirs
          /usr/lib/tmac  ~/newpaper/references  ~
          % popd
          ~/newpaper/references  ~
          % popd
          ~
          %

     The list is printed in a horizontal line, reading left to  right,
     with  a  tilde  (~) as shorthand for your home directory--in this
     case `/home/bill'.  The directory stack is printed whenever there
     is more than one entry on it and it changes.  It is also  printed
     by  a  _d_i_r_s command.  _D_i_r_s is usually faster and more informative
     than _p_w_d since it shows the current working directory as well  as
     any other directories remembered in the stack.

          The  _p_u_s_h_d  command  with no argument alternates the current
     directory with  the  first  directory  in  the  list.   The  `pop









     An Introduction to the C shell                           USD:4-31


     directory'  _p_o_p_d  command  without an argument returns you to the
     directory you were in prior to the current  one,  discarding  the
     previous  current directory from the stack (forgetting it).  Typ-
     ing _p_o_p_d several times in a series takes you backward through the
     directories you had been in (changed to) by  the  _p_u_s_h_d  command.
     There  are other options to _p_u_s_h_d and _p_o_p_d to manipulate the con-
     tents of the directory stack and to change to directories not  at
     the top of the stack; see the _c_s_h manual page for details.

          Since  the  shell  remembers  the working directory in which
     each job was started, it warns you when you might be confused  by
     restarting  a job in the foreground which has a different working
     directory than the current working directory of the shell.   Thus
     if  you  start  a background job, then change the shell's working
     directory and then cause the background job to run in  the  fore-
     ground,  the  shell  warns  you that the working directory of the
     currently running foreground job is different from  that  of  the
     shell.

          % dirs -l
          /home/bill
          % cd myproject
          % dirs
          ~/myproject
          % ed prog.c
          1143
          ^Z
          Suspended
          % cd ..
          % ls
          myproject
          textfile
          % fg
          ed prog.c (wd: ~/myproject)

     This  way  the shell warns you when there is an implied change of
     working directory, even though no cd command was issued.  In  the
     above example the `ed' job was still in `/home/bill/project' even
     though  the shell had changed to `/home/bill'.  A similar warning
     is given when such a foreground job terminates  or  is  suspended
     (using  the  STOP signal) since the return to the shell again im-
     plies a change of working directory.

          % fg
          ed prog.c (wd: ~/myproject)
           . . . after some editing
          q
          (wd now: ~)
          %

     These messages are sometimes confusing if you use  programs  that
     change their own working directories, since the shell only remem-
     bers  which  directory  a job is started in, and assumes it stays
     there.  The `-l' option of _j_o_b_s will type the  working  directory









     USD:4-32                           An Introduction to the C shell


     of  suspended  or  background  jobs when it is different from the
     current working directory of the shell.

     22..88..  UUsseeffuull bbuuiilltt--iinn ccoommmmaannddss

          We now give a few of the useful  built-in  commands  of  the
     shell describing how they are used.

          The  _a_l_i_a_s  command  described  above  is used to assign new
     aliases and to show the existing aliases.  With no  arguments  it
     prints  the current aliases.  It may also be given only one argu-
     ment such as

          alias ls

     to show the current alias for, e.g., `ls'.

          The _e_c_h_o command prints its arguments.  It is often used  in
     _s_h_e_l_l  _s_c_r_i_p_t_s  or as an interactive command to see what filename
     expansions will produce.

          The _h_i_s_t_o_r_y command will show the contents  of  the  history
     list.   The  numbers given with the history events can be used to
     reference previous events which are difficult to reference  using
     the  contextual  mechanisms  introduced  above.   There is also a
     shell variable called _p_r_o_m_p_t.  By placing a `!' character in  its
     value  the  shell will there substitute the number of the current
     command in the history list.  You can use this number to refer to
     this command in a history substitution.  Thus you could

          set prompt='\! % '

     Note that the `!' character had to be _e_s_c_a_p_e_d  here  even  within
     `'' characters.

          The  _l_i_m_i_t  command  is  used  to restrict use of resources.
     With no arguments it prints the current limitations:

          cputime   unlimited
          filesize  unlimited
          datasize  5616 kbytes
          stacksize 512 kbytes
          coredumpsizeunlimited

     Limits can be set, e.g.:

          limit coredumpsize 128k

     Most reasonable units abbreviations will work; see the _c_s_h manual
     page for more details.

          The _l_o_g_o_u_t command can be used to terminate  a  login  shell
     which has _i_g_n_o_r_e_e_o_f set.










     An Introduction to the C shell                           USD:4-33


          The  _r_e_h_a_s_h command causes the shell to recompute a table of
     where commands are located.  This is necessary if you add a  com-
     mand  to  a directory in the current shell's search path and wish
     the shell to find it, since otherwise the hashing  algorithm  may
     tell the shell that the command wasn't in that directory when the
     hash table was computed.

          The  _r_e_p_e_a_t  command can be used to repeat a command several
     times.  Thus to make 5 copies of the file _o_n_e in  the  file  _f_i_v_e
     you could do

          repeat 5 cat one >> five

          The _s_e_t_e_n_v command can be used to set variables in the envi-
     ronment.  Thus

          setenv TERM adm3a

     will  set  the value of the environment variable TERM to `adm3a'.
     A user program _p_r_i_n_t_e_n_v exists which will print out the  environ-
     ment.  It might then show:

          % printenv
          HOME=/home/bill
          SHELL=/bin/csh
          PATH=:/bin:/usr/bin:/usr/local/bin
          TERM=adm3a
          USER=bill
          %

          The _s_o_u_r_c_e command can be used to force the current shell to
     read commands from a file.  Thus

          source .cshrc

     can  be  used  after editing in a change to the _._c_s_h_r_c file which
     you wish to take effect right away.

          The _t_i_m_e command can be used to cause a command to be  timed
     no matter how much CPU time it takes.  Thus

          % time cp /etc/rc /home/bill/rc
          0.0u 0.1s 0:01 8% 2+1k 3+2io 1pf+0w
          % time wc /etc/rc /home/bill/rc
               52    178   1347 /etc/rc
               52    178   1347 /home/bill/rc
              104    356   2694 total
          0.1u 0.1s 0:00 13% 3+3k 5+3io 7pf+0w
          %

     indicates  that  the  _c_p command used a negligible amount of user
     time (u) and about 1/10th of a system time (s); the elapsed  time
     was  1  second  (0:01),  there  was an average memory usage of 2k
     bytes of program space and 1k bytes of data space  over  the  CPU









     USD:4-34                           An Introduction to the C shell


     time  involved  (2+1k);  the program did three disk reads and two
     disk writes (3+2io), and took one page fault and was not  swapped
     (1pf+0w).   The  word count command _w_c on the other hand used 0.1
     seconds of user time and 0.1 seconds of system time in less  than
     a  second  of  elapsed time.  The percentage `13%' indicates that
     over the period when it was active the command `wc' used an aver-
     age of 13 percent of the available CPU cycles of the machine.

          The _u_n_a_l_i_a_s and _u_n_s_e_t commands can be used to remove aliases
     and variable definitions from the  shell,  and  _u_n_s_e_t_e_n_v  removes
     variables from the environment.

     22..99..  WWhhaatt eellssee??

          This  concludes the basic discussion of the shell for termi-
     nal users.  There are more features of the shell to be  discussed
     here,  and  all features of the shell are discussed in its manual
     pages.  One useful feature which is discussed later is the  _f_o_r_e_-
     _a_c_h  built-in  command  which can be used to run the same command
     sequence with a number of different arguments.

          If you intend to use UNIX a lot you should look through  the
     rest  of this document and the csh manual pages (section1) to be-
     come familiar with the other facilities which  are  available  to
     you.






































     An Introduction to the C shell                           USD:4-35


     33..  SShheellll ccoonnttrrooll ssttrruuccttuurreess aanndd ccoommmmaanndd ssccrriippttss

     33..11..  IInnttrroodduuccttiioonn

          It  is  possible  to  place  commands  in files and to cause
     shells to be invoked to read  and  execute  commands  from  these
     files, which are called _s_h_e_l_l _s_c_r_i_p_t_s.  We here detail those fea-
     tures of the shell useful to the writers of such scripts.

     33..22..  MMaakkee

          It  is  important  to  first note what shell scripts are _n_o_t
     useful for.  There is a program called _m_a_k_e which is very  useful
     for  maintaining  a  group of related files or performing sets of
     operations on related files.  For instance a large  program  con-
     sisting  of one or more files can have its dependencies described
     in a _m_a_k_e_f_i_l_e which contains definitions of the commands used  to
     create  these different files when changes occur.  Definitions of
     the means for printing listings, cleaning  up  the  directory  in
     which the files reside, and installing the resultant programs are
     easily,  and  most  appropriately, placed in this _m_a_k_e_f_i_l_e.  This
     format is superior and preferable to maintaining a group of shell
     procedures to maintain these files.

          Similarly when working on a document a _m_a_k_e_f_i_l_e may be  cre-
     ated  which defines how different versions of the document are to
     be created and which options of _n_r_o_f_f or _t_r_o_f_f are appropriate.

     33..33..  IInnvvooccaattiioonn aanndd tthhee aarrggvv vvaarriiaabbllee

          A _c_s_h command script may be interpreted by saying

          % csh script ...

     where _s_c_r_i_p_t is the name of the file containing a  group  of  _c_s_h
     commands  and  `...' is replaced by a sequence of arguments.  The
     shell places these arguments in the variable _a_r_g_v and then begins
     to read commands from the  script.   These  parameters  are  then
     available through the same mechanisms which are used to reference
     any other shell variables.

          If you make the file `script' executable by doing

          chmod 755 script

     and  place  a  shell comment at the beginning of the shell script
     (i.e. begin the file with a `#' character) then a `/bin/csh' will
     automatically be invoked to execute `script' when you type

          script

     If the file does not begin with a `#'  then  the  standard  shell
     `/bin/sh' will be used to execute it.  This allows you to convert
     your older shell scripts to use _c_s_h at your convenience.









     USD:4-36                           An Introduction to the C shell


     33..44..  VVaarriiaabbllee ssuubbssttiittuuttiioonn

          After  each input line is broken into words and history sub-
     stitutions are done on it, the input line is parsed into distinct
     commands.  Before each command is executed a  mechanism  know  as
     _v_a_r_i_a_b_l_e _s_u_b_s_t_i_t_u_t_i_o_n is done on these words.  Keyed by the char-
     acter  `$'  this  substitution replaces the names of variables by
     their values.  Thus

          echo $argv

     when placed in a command script would cause the current value  of
     the variable _a_r_g_v to be echoed to the output of the shell script.
     It is an error for _a_r_g_v to be unset at this point.

          A  number of notations are provided for accessing components
     and attributes of variables.  The notation

          $?name

     expands to `1' if name is _s_e_t or to `0' if name is not  _s_e_t.   It
     is the fundamental mechanism used for checking whether particular
     variables  have  been assigned values.  All other forms of refer-
     ence to undefined variables cause errors.

          The notation

          $#name

     expands to the number of elements in the variable _n_a_m_e.  Thus

          % set argv=(a b c)
          % echo $?argv
          1
          % echo $#argv
          3
          % unset argv
          % echo $?argv
          0
          % echo $argv
          argv: Undefined variable.
          %

          It is also possible to access the components of  a  variable
     which has several values.  Thus

          $argv[1]

     gives  the  first  component of _a_r_g_v or in the example above `a'.
     Similarly

          $argv[$#argv]











     An Introduction to the C shell                           USD:4-37


     would give `c', and

          $argv[1-2]

     would give `a b'. Other notations useful in shell scripts are

          $_n

     where _n is an integer as a shorthand for

          $argv[_n]

     the _n_t_h parameter and

          $*

     which is a shorthand for

          $argv

     The form

          $$

     expands to the process number of the current shell.   Since  this
     process  number is unique in the system it can be used in genera-
     tion of unique temporary file names.  The form

          $<

     is quite special and is replaced by the next line of  input  read
     from  the  shell's standard input (not the script it is reading).
     This is useful for writing shell scripts  that  are  interactive,
     reading  commands  from  the  terminal,  or  even writing a shell
     script that acts as a filter, reading lines from its input  file.
     Thus the sequence

          echo 'yes or no?\c'
          set a=($<)

     would  write  out  the  prompt `yes or no?' without a newline and
     then read the answer into the variable `a'.  In this  case  `$#a'
     would  be  `0'  if  either  a  blank line or end-of-file (^D) was
     typed.

          One minor difference between `$_n' and `$argv[_n]'  should  be
     noted  here.  The form `$argv[_n]' will yield an error if _n is not
     in the range `1-$#argv' while `$n' will never  yield  an  out  of
     range  subscript  error.   This is for compatibility with the way
     older shells handled parameters.

          Another important point is that it is never an error to give
     a subrange of the form `n-'; if there are less than _n  components
     of  the given variable then no words are substituted.  A range of









     USD:4-38                           An Introduction to the C shell


     the form `m-n' likewise returns an empty vector without giving an
     error when _m exceeds the number of elements of  the  given  vari-
     able, provided the subscript _n is in range.

     33..55..  EExxpprreessssiioonnss

          In  order for interesting shell scripts to be constructed it
     must be possible to evaluate expressions in the  shell  based  on
     the  values of variables.  In fact, all the arithmetic operations
     of the language C are available in the shell with the same prece-
     dence that they have in C.  In particular,  the  operations  `=='
     and  `!='  compare strings and the operators `&&' and `||' imple-
     ment the boolean and/or operations.  The special  operators  `=~'
     and  `!~'  are similar to `==' and `!=' except that the string on
     the right side can have pattern matching characters (like *, ? or
     []) and the test is whether the string on the  left  matches  the
     pattern on the right.

          The shell also allows file enquiries of the form

          -? filename

     where  `?'  is replace by a number of single characters.  For in-
     stance the expression primitive

          -e filename

     tell whether the file `filename' exists.  Other  primitives  test
     for  read,  write and execute access to the file, whether it is a
     directory, or has non-zero length.

          It is possible to test whether  a  command  terminates  nor-
     mally,  by  a  primitive  of the form `{ command }' which returns
     true, i.e. `1' if the command succeeds exiting normally with exit
     status 0, or `0' if the command  terminates  abnormally  or  with
     exit status non-zero.  If more detailed information about the ex-
     ecution  status  of a command is required, it can be executed and
     the variable `$status'  examined  in  the  next  command.   Since
     `$status'  is set by every command, it is very transient.  It can
     be saved if it is inconvenient to use it only in the single imme-
     diately following command.

          For a full list of expression components available  see  the
     manual section for the shell.

     33..66..  SSaammppllee sshheellll ssccrriipptt

          A  sample  shell  script  which  makes use of the expression















     An Introduction to the C shell                           USD:4-39


     mechanism of the shell and some of its control structure follows:

          % cat copyc
          #
          # Copyc copies those C programs in the specified list
          # to the directory ~/backup if they differ from the files
          # already in ~/backup
          #
          set backdir = ~/backup
          set noglob
          foreach i ($argv)

                  if ($i !~ *.c) continue  # not a .c file so do nothing

                  if (! -r $backdir/$i:t) then
                          echo $i:t not in backup... not cp'ed
                          continue
                  endif

                  cmp -s $i $backdir/$i:t # to set $status

                  if ($status != 0) then
                          echo new backup of $i
                          cp $i $backdir/$i:t
                  endif
          end

          This script makes use of the _f_o_r_e_a_c_h command,  which  causes
     the  shell  to  execute  the commands between the _f_o_r_e_a_c_h and the
     matching _e_n_d for each of the values given  between  `('  and  `)'
     with  the named variable, in this case `i' set to successive val-
     ues in the list.  Within this loop we may use the  command  _b_r_e_a_k
     to  stop executing the loop and _c_o_n_t_i_n_u_e to prematurely terminate
     one iteration and begin the next.  After the _f_o_r_e_a_c_h loop the it-
     eration variable (_i in this case) has the value at the last iter-
     ation.

          We set the variable _n_o_g_l_o_b here to prevent  filename  expan-
     sion of the members of _a_r_g_v.  This is a good idea, in general, if
     the  arguments to a shell script are filenames which have already
     been expanded or if the arguments may contain filename  expansion
     metacharacters.   It  is also possible to quote each use of a `$'
     variable expansion, but this is harder and less reliable.

          The other control construct used here is a statement of  the
     form

          iiff ( expression ) tthheenn
               command
               ...
          eennddiiff

     The  placement  of  the  keywords here is nnoott flexible due to the










     USD:4-40                           An Introduction to the C shell


     current implementation of the shell.*

          The  shell does have another form of the if statement of the
     form

          iiff ( expression ) ccoommmmaanndd

     which can be written

          iiff ( expression ) \
               command

     Here we have escaped the newline for the sake of appearance.  The
     command must not involve `|', `&' or `;' and must not be  another
     control command.  The second form requires the final `\' to iimmmmee--
     ddiiaatteellyy precede the end-of-line.

          The  more  general _i_f statements above also admit a sequence
     of _e_l_s_e_-_i_f pairs followed by a single _e_l_s_e and an _e_n_d_i_f, e.g.:

          iiff ( expression ) tthheenn
               commands
          eellssee iiff (expression ) tthheenn
               commands
          ...

          eellssee
               commands
          eennddiiff

          Another important mechanism used in shell scripts is the `:'
     modifier.  We can use the modifier `:r' here to extract a root of
     a filename or `:e' to extract the _e_x_t_e_n_s_i_o_n.  Thus if  the  vari-
     able _i has the value `/mnt/foo.bar' then

          % echo $i $i:r $i:e
          /mnt/foo.bar /mnt/foo bar
          %

     shows  how  the  `:r' modifier strips off the trailing `.bar' and
     the `:e' modifier leaves only the `bar'.   Other  modifiers  will
     -----------
     *The following two formats are not currently  acceptable  to
     the shell:

          iiff ( expression )        # WWoonn''tt wwoorrkk!!
          tthheenn
               command
               ...
          eennddiiff

     and

          iiff ( expression ) tthheenn command eennddiiff         # WWoonn''tt wwoorrkk









     An Introduction to the C shell                           USD:4-41


     take  off  the last component of a pathname leaving the head `:h'
     or all but the last component of  a  pathname  leaving  the  tail
     `:t'.   These  modifiers  are  fully  described in the _c_s_h manual
     pages in the User's Reference Manual.  It is also possible to use
     the _c_o_m_m_a_n_d _s_u_b_s_t_i_t_u_t_i_o_n mechanism described in  the  next  major
     section  to  perform modifications on strings to then reenter the
     shell's environment.  Since each usage of this mechanism involves
     the creation of a new process, it is much more expensive  to  use
     than the `:' modification mechanism.**  Finally, we note that the
     character `#' lexically  introduces  a  shell  comment  in  shell
     scripts  (but  not from the terminal).  All subsequent characters
     on the input line after a `#' are discarded by the  shell.   This
     character  can be quoted using `'' or `\' to place it in an argu-
     ment word.

     33..77..  OOtthheerr ccoonnttrrooll ssttrruuccttuurreess

          The shell also has control structures _w_h_i_l_e and _s_w_i_t_c_h simi-
     lar to those of C.  These take the forms

          wwhhiillee ( expression )
               commands
          eenndd

     and

          sswwiittcchh ( word )

          ccaassee str1:
               commands
               bbrreeaakkssww

           ...

          ccaassee strn:
               commands
               bbrreeaakkssww

          ddeeffaauulltt::
               commands
               bbrreeaakkssww

          eennddssww

     -----------
     ** It is also important to note that the current implementa-
     tion  of  the  shell limits the number of `:' modifiers on a
     `$' substitution to 1.  Thus

          % echo $i $i:h:t
          /a/b/c /a/b:t
          %

     does not do what one would expect.









     USD:4-42                           An Introduction to the C shell


     For details see the manual section for _c_s_h.  C programmers should
     note that we use _b_r_e_a_k_s_w to exit from a _s_w_i_t_c_h while _b_r_e_a_k  exits
     a _w_h_i_l_e or _f_o_r_e_a_c_h loop.  A common mistake to make in _c_s_h scripts
     is to use _b_r_e_a_k rather than _b_r_e_a_k_s_w in switches.

          Finally,  _c_s_h  allows  a _g_o_t_o statement, with labels looking
     like they do in C, i.e.:

          loop:
               commands
               ggoottoo loop

     33..88..  SSuuppppllyyiinngg iinnppuutt ttoo ccoommmmaannddss

          Commands run from shell scripts receive by default the stan-
     dard input of the shell which is running  the  script.   This  is
     different  from  previous  shells  running under UNIX.  It allows
     shell scripts to fully participate in pipelines, but mandates ex-
     tra notation for commands which are to take inline data.

          Thus we need a metanotation for  supplying  inline  data  to
     commands  in  shell scripts.  As an example, consider this script
     which runs the editor to delete leading blanks from the lines  in
     each argument file:

          % cat deblank
          # deblank -- remove leading blanks
          foreach i ($argv)
          ed - $i << 'EOF'
          1,$s/^[ ]*//
          w
          q
          'EOF'
          end
          %

     The  notation `<< 'EOF'' means that the standard input for the _e_d
     command is to come from the text in the shell script file  up  to
     the  next  line consisting of exactly `'EOF''.  The fact that the
     `EOF' is enclosed in `''  characters,  i.e.  quoted,  causes  the
     shell  to  not  perform  variable substitution on the intervening
     lines.  In general, if any part of the word  following  the  `<<'
     which  the  shell  uses  to terminate the text to be given to the
     command is quoted then these substitutions will not be performed.
     In this case since we used the form `1,$' in our editor script we
     needed to ensure that this `$' was not variable substituted.   We
     could  also  have  ensured  this by preceding the `$' here with a
     `\', i.e.:

          1,\$s/^[ ]*//

     but quoting the `EOF'  terminator  is  a  more  reliable  way  of
     achieving the same thing.










     An Introduction to the C shell                           USD:4-43


     33..99..  CCaattcchhiinngg iinntteerrrruuppttss

          If  our shell script creates temporary files, we may wish to
     catch interruptions of the shell script so that we can  clean  up
     these files.  We can then do

          onintr label

     where  _l_a_b_e_l  is  a label in our program.  If an interrupt is re-
     ceived the shell will do a `goto label' and  we  can  remove  the
     temporary files and then do an _e_x_i_t command (which is built in to
     the  shell)  to  exit  from the shell script.  If we wish to exit
     with a non-zero status we can do

          exit(1)

     e.g. to exit with status `1'.

     33..1100..  WWhhaatt eellssee??

          There are other features of the shell useful to  writers  of
     shell  procedures.   The _v_e_r_b_o_s_e and _e_c_h_o options and the related
     _-_v and _-_x command line options can be used to help trace the  ac-
     tions  of the shell.  The _-_n option causes the shell only to read
     commands and not to execute them and may sometimes be of use.

          One other thing to note is that _c_s_h will not  execute  shell
     scripts  which do not begin with the character `#', that is shell
     scripts that  do  not  begin  with  a  comment.   Similarly,  the
     `/bin/sh'  on  your  system  may well defer to `csh' to interpret
     shell scripts which begin with `#'.  This  allows  shell  scripts
     for both shells to live in harmony.

          There  is  also  another quotation mechanism using `"' which
     allows only some of the expansion mechanisms we have so far  dis-
     cussed  to  occur  on  the  quoted string and serves to make this
     string into a single word as `'' does.


























     USD:4-44                           An Introduction to the C shell


     44..  OOtthheerr,, lleessss ccoommmmoonnllyy uusseedd,, sshheellll ffeeaattuurreess

     44..11..  LLooooppss aatt tthhee tteerrmmiinnaall;; vvaarriiaabblleess aass vveeccttoorrss

          It is occasionally useful to use the _f_o_r_e_a_c_h control  struc-
     ture  at  the  terminal  to aid in performing a number of similar
     commands.  For instance, there were at one point three shells  in
     use  on the Cory UNIX system at Cory Hall, `/bin/sh', `/bin/nsh',
     and `/bin/csh'.  To count the number of persons using each  shell
     one could have issued the commands

          % grep -c csh$ /etc/passwd
          27
          % grep -c nsh$ /etc/passwd
          128
          % grep -c -v sh$ /etc/passwd
          430
          %

     Since  these  commands  are very similar we can use _f_o_r_e_a_c_h to do
     this more easily.

          % foreach i ('sh$' 'csh$' '-v sh$')
          ? grep -c $i /etc/passwd
          ? end
          27
          128
          430
          %

     Note here that the shell prompts for input with `? ' when reading
     the body of the loop.

          Very useful with loops are variables which contain lists  of
     filenames or other words.  You can, for example, do

          % set a=(`ls`)
          % echo $a
          csh.n csh.rm
          % ls
          csh.n
          csh.rm
          % echo $#a
          2
          %

     The  _s_e_t command here gave the variable _a a list of all the file-
     names in the current directory as value.   We  can  then  iterate
     over these names to perform any chosen function.

          The  output  of a command within ``' characters is converted
     by the shell to a list of words.  You  can  also  place  the  ``'
     quoted string within `"' characters to take each (non-empty) line
     as  a  component of the variable; preventing the lines from being









     An Introduction to the C shell                           USD:4-45


     split into words at blanks and  tabs.   A  modifier  `:x'  exists
     which  can be used later to expand each component of the variable
     into another variable splitting it into separate words at  embed-
     ded blanks and tabs.

     44..22..  BBrraacceess {{ ...... }} iinn aarrgguummeenntt eexxppaannssiioonn

          Another  form  of filename expansion, alluded to before, in-
     volves the characters `{' and `}'.  These characters specify that
     the contained strings, separated by `,', are to be  consecutively
     substituted  into  the  containing characters and the results ex-
     panded left to right.  Thus

          A{str1,str2,...strn}B

     expands to

          Astr1B Astr2B ... AstrnB

     This expansion occurs before the other filename  expansions,  and
     may  be  applied  recursively (i.e. nested).  The results of each
     expanded string are sorted separately, left to right order  being
     preserved.   The resulting filenames are not required to exist if
     no other expansion mechanisms are used.   This  means  that  this
     mechanism  can  be used to generate arguments which are not file-
     names, but which have common parts.

          A typical use of this would be

          mkdir ~/{hdrs,retrofit,csh}

     to make subdirectories `hdrs', `retrofit' and `csh' in your  home
     directory.   This mechanism is most useful when the common prefix
     is longer than in this example, i.e.

          chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}

     44..33..  CCoommmmaanndd ssuubbssttiittuuttiioonn

          A command enclosed in ``' characters is replaced,  just  be-
     fore  filenames  are  expanded,  by the output from that command.
     Thus it is possible to do

          set pwd=`pwd`

     to save the current directory in the variable _p_w_d or to do

          ex `grep -l TRACE *.c`

     to run the editor _e_x supplying as  arguments  those  files  whose
     names end in `.c' which have the string `TRACE' in them.*
     -----------
     *Command expansion also occurs in input redirected with `<<'
     and  within  `"' quotations.  Refer to the shell manual sec-









     USD:4-46                           An Introduction to the C shell


     44..44..  OOtthheerr ddeettaaiillss nnoott ccoovveerreedd hheerree

          In particular circumstances it may be necessary to know  the
     exact  nature  and  order of different substitutions performed by
     the shell.  The exact meaning of certain combinations  of  quota-
     tions  is  also occasionally important.  These are detailed fully
     in its manual section.

          The shell has a number of command line option  flags  mostly
     of  use  in  writing  UNIX programs, and debugging shell scripts.
     See the csh(1) manual section for a list of these options.









































     -----------
     tion for full details.









     An Introduction to the C shell                           USD:4-47


     AAppppeennddiixx -- SSppeecciiaall cchhaarraacctteerrss

     The following table lists the special characters of _c_s_h  and  the
     UNIX  system,  giving for each the section(s) in which it is dis-
     cussed.  A number of these characters also have  special  meaning
     in expressions.  See the _c_s_h manual section for a complete list.

     Syntactic metacharacters

          ;    2.4  separates commands to be executed sequentially
          |    1.5  separates commands in a pipeline
          ( )  2.2,3.6   brackets expressions and variable values
          &    2.5  follows commands to be executed without waiting for completion

     Filename metacharacters

          /    1.6  separates components of a file's pathname
          .    1.6  separates root parts of a file name from extensions
          ?    1.6  expansion character matching any single character
          *    1.6  expansion character matching any sequence of characters
          [ ]  1.6  expansion sequence matching any single character from a set
          ~    1.6  used at the beginning of a filename to indicate home directories
          { }  4.2  used to specify groups of arguments with common parts

     Quotation metacharacters

          \    1.7  prevents meta-meaning of following single character
          '    1.7  prevents meta-meaning of a group of characters
          "    4.3  like ', but allows variable and command expansion

     Input/output metacharacters

          <    1.5  indicates redirected input
          >    1.3  indicates redirected output

     Expansion/substitution metacharacters

          $    3.4  indicates variable substitution
          !    2.3  indicates history substitution
          :    3.6  precedes substitution modifiers
          ^    2.3  used in special forms of history substitution
          `    4.3  indicates command substitution

     Other metacharacters

          #    1.3,3.6   begins scratch file names; indicates shell comments
          -    1.2  prefixes option (flag) arguments to commands
          %    2.6  prefixes job name specifications

     GGlloossssaarryy

          This  glossary  lists the most important terms introduced in
     the introduction to the shell and gives references to sections of
     the  shell  document  for   further   information   about   them.









     USD:4-48                           An Introduction to the C shell


     References  of  the form pr(1) indicate that the command _p_r is in
     the UNIX User Reference manual in section 1.  You can look at  an
     online copy of its manual page by doing

          man 1 pr

     References  of  the form (2.5) indicate that more information can
     be found in section 2.5 of this manual.

     ..              Your current directory has the name `.' as well as
                    the name printed by  the  command  _p_w_d;  see  also
                    _d_i_r_s.   The  current  directory `.' is usually in-
                    cluded as a _c_o_m_p_o_n_e_n_t of the search path contained
                    in the variable _p_a_t_h.  The character `.'  is  also
                    used  in separating _c_o_m_p_o_n_e_n_t_s of filenames (1.6).
                    The character `.' at the beginning of a  _c_o_m_p_o_n_e_n_t
                    of a _p_a_t_h_n_a_m_e is treated specially and not matched
                    by the _f_i_l_e_n_a_m_e _e_x_p_a_n_s_i_o_n metacharacters `?', `*',
                    and `[' `]' pairs (1.6).

     ....             Each  directory  has  a file `..' in it which is a
                    reference to its parent directory.  After changing
                    into the directory with _c_h_d_i_r, i.e.

                         chdir paper

                    you can return to the parent directory by doing

                         chdir ..

                    The current directory is printed by _p_w_d (2.7).

     a.out          Compilers which create  executable  images  create
                    them, by default, in the file _a_._o_u_t.  for histori-
                    cal reasons (2.3).

     absolute pathname
                    A  _p_a_t_h_n_a_m_e  which  begins  with a `/' is _a_b_s_o_l_u_t_e
                    since it specifies the _p_a_t_h  of  directories  from
                    the  beginning  of  the  entire directory system -
                    called the _r_o_o_t directory.   _P_a_t_h_n_a_m_es  which  are
                    not  _a_b_s_o_l_u_t_e  are called _r_e_l_a_t_i_v_e (see definition
                    of _r_e_l_a_t_i_v_e _p_a_t_h_n_a_m_e) (1.6).

     alias          An _a_l_i_a_s specifies a shorter or different name for
                    a UNIX command, or a transformation on  a  command
                    to  be  performed  in  the shell.  The shell has a
                    command _a_l_i_a_s which establishes  _a_l_i_a_s_e_s  and  can
                    print  their  current values.  The command _u_n_a_l_i_a_s
                    is used to remove _a_l_i_a_s_e_s (2.4).

     argument       Commands in UNIX receive a list of _a_r_g_u_m_e_n_t words.











     An Introduction to the C shell                           USD:4-49


                    Thus the command

                         echo a b c

                    consists of the _c_o_m_m_a_n_d _n_a_m_e `echo' and three  _a_r_-
                    _g_u_m_e_n_t  words  `a', `b' and `c'.  The set of _a_r_g_u_-
                    _m_e_n_t_s after the _c_o_m_m_a_n_d _n_a_m_e is said to be the _a_r_-
                    _g_u_m_e_n_t _l_i_s_t of the command (1.1).

     argv           The list of arguments to a command written in  the
                    shell language (a shell script or shell procedure)
                    is  stored  in  a  variable called _a_r_g_v within the
                    shell.  This name is taken from  the  conventional
                    name in the C programming language (3.4).

     background     Commands  started without waiting for them to com-
                    plete are called _b_a_c_k_g_r_o_u_n_d commands (2.6).

     base           A filename is sometimes thought of  as  consisting
                    of  a  _b_a_s_e part, before any `.' character, and an
                    _e_x_t_e_n_s_i_o_n - the part after the `.'.  See  _f_i_l_e_n_a_m_e
                    and _e_x_t_e_n_s_i_o_n (1.6) and basename(1).

     bg             The  _b_g command causes a _s_u_s_p_e_n_d_e_d job to continue
                    execution in the _b_a_c_k_g_r_o_u_n_d (2.6).

     bin            A directory containing binaries  of  programs  and
                    shell scripts to be executed is typically called a
                    _b_i_n  directory.   The standard system _b_i_n directo-
                    ries are `/bin' containing the most  heavily  used
                    commands  and `/usr/bin' which contains most other
                    user programs.  Locally written programs  live  in
                    `/usr/local/bin'.  Games are kept in the directory
                    `/usr/games'.   You  can place binaries in any di-
                    rectory.  If you wish to execute them  often,  the
                    name  of  the directories should be a _c_o_m_p_o_n_e_n_t of
                    the variable _p_a_t_h.

     break          _B_r_e_a_k is a builtin command used to exit from loops
                    within the control structure of the shell (3.7).

     breaksw        The _b_r_e_a_k_s_w builtin command is used to exit from a
                    _s_w_i_t_c_h control structure, like a _b_r_e_a_k exits  from
                    loops (3.7).

     builtin        A command executed directly by the shell is called
                    a  _b_u_i_l_t_i_n command.  Most commands in UNIX are not
                    built into the shell, but rather exist as files in
                    _b_i_n directories.  These  commands  are  accessible
                    because  the  directories in which they reside are
                    named in the _p_a_t_h variable.

     case           A _c_a_s_e command is used as  a  label  in  a  _s_w_i_t_c_h
                    statement   in   the  shell's  control  structure,









     USD:4-50                           An Introduction to the C shell


                    similar to that of the language  C.   Details  are
                    given in the shell documentation csh(1) (3.7).

     cat            The  _c_a_t  program concatenates a list of specified
                    files on the _s_t_a_n_d_a_r_d _o_u_t_p_u_t.  It is usually  used
                    to  look  at  the contents of a single file on the
                    terminal, to `cat a file' (1.8, 2.3).

     cd             The _c_d command is used to change the  _w_o_r_k_i_n_g  _d_i_-
                    _r_e_c_t_o_r_y.  With no arguments, _c_d changes your _w_o_r_k_-
                    _i_n_g  _d_i_r_e_c_t_o_r_y  to  be  your  _h_o_m_e directory (2.4,
                    2.7).

     chdir          The _c_h_d_i_r command is a synonym for _c_d.  _C_d is usu-
                    ally used because it is easier to type.

     chsh           The _c_h_s_h command is used to change the shell which
                    you use on UNIX.  By default, you use a  different
                    version  of  the shell which resides in `/bin/sh'.
                    You can change your shell to `/bin/csh' by doing

                         chsh -s /bin/csh your-login-name

                    Thus I would do

                         chsh -s /bin/csh bill

                    It is only necessary to do this  once.   The  next
                    time  you log in to UNIX after doing this command,
                    you will be using _c_s_h rather  than  the  shell  in
                    `/bin/sh' (1.9).

     cmp            _C_m_p is a program which compares files.  It is usu-
                    ally  used on binary files, or to see if two files
                    are identical (3.6).  For comparing text files the
                    program _d_i_f_f, described in diff(1) is used.

     command        A function performed by the system, either by  the
                    shell (a builtin _c_o_m_m_a_n_d) or by a program residing
                    in  a  file in a directory within the UNIX system,
                    is called a _c_o_m_m_a_n_d (1.1).

     command name
                    When a command is issued, it consists of a _c_o_m_m_a_n_d
                    _n_a_m_e, which is the first word of the command, fol-
                    lowed by arguments.  The  convention  on  UNIX  is
                    that  the  first word of a command names the func-
                    tion to be performed (1.1).

     command substitution
                    The replacement of a command enclosed in ``' char-
                    acters by the  text  output  by  that  command  is
                    called _c_o_m_m_a_n_d _s_u_b_s_t_i_t_u_t_i_o_n (4.3).










     An Introduction to the C shell                           USD:4-51


     component      A  part  of  a  _p_a_t_h_n_a_m_e between `/' characters is
                    called a _c_o_m_p_o_n_e_n_t of that _p_a_t_h_n_a_m_e.   A  variable
                    which  has  multiple  strings  as value is said to
                    have several _c_o_m_p_o_n_e_n_ts; each string is  a  _c_o_m_p_o_-
                    _n_e_n_t of the variable.

     continue       A  builtin  command  which causes execution of the
                    enclosing _f_o_r_e_a_c_h or _w_h_i_l_e loop  to  cycle  prema-
                    turely.   Similar  to  the _c_o_n_t_i_n_u_e command in the
                    programming language C (3.6).

     control-       Certain special characters, called _c_o_n_t_r_o_l charac-
                    ters, are produced by holding down the CONTROL key
                    on your terminal and simultaneously  pressing  an-
                    other  character,  much like the SHIFT key is used
                    to produce upper case characters.  Thus  _c_o_n_t_r_o_l_-c
                    is  produced by holding down the CONTROL key while
                    pressing the `c'  key.   Usually  UNIX  prints  an
                    caret  (^)  followed  by  the corresponding letter
                    when you type a _c_o_n_t_r_o_l character (e.g.  `^C'  for
                    _c_o_n_t_r_o_l_-c (1.8).

     core dump      When  a  program terminates abnormally, the system
                    places an image of its current  state  in  a  file
                    named `core'.  This _c_o_r_e _d_u_m_p can be examined with
                    the  system  debugger gdb(1) in order to determine
                    what went wrong with the program  (1.8).   If  the
                    shell produces a message of the form

                         Segmentation fault (core dumped)

                    (where `Segmentation fault' is only one of several
                    possible  messages), you should report this to the
                    author of the program or a  system  administrator,
                    saving the `core' file.

     cp             The _c_p (copy) program is used to copy the contents
                    of  one  file into another file.  It is one of the
                    most commonly used UNIX commands (1.6).

     csh            The name of the shell program that  this  document
                    describes.

     .cshrc         The  file _._c_s_h_r_c in your _h_o_m_e directory is read by
                    each shell as it begins execution.  It is  usually
                    used  to  change  the setting of the variable _p_a_t_h
                    and to set _a_l_i_a_s parameters which are to take  ef-
                    fect globally (2.1).

     cwd            The  _c_w_d  variable in the shell holds the _a_b_s_o_l_u_t_e
                    _p_a_t_h_n_a_m_e of the current _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y.  It  is
                    changed by the shell whenever your current _w_o_r_k_i_n_g
                    _d_i_r_e_c_t_o_r_y changes and should not be changed other-
                    wise (2.2).









     USD:4-52                           An Introduction to the C shell


     date           The  _d_a_t_e command prints the current date and time
                    (1.3).

     debugging      _D_e_b_u_g_g_i_n_g is the process of correcting mistakes in
                    programs and shell scripts.  The shell has several
                    options and variables which may be used to aid  in
                    shell _d_e_b_u_g_g_i_n_g (4.4).

     default:       The  label  _d_e_f_a_u_l_t_:  is  used within shell _s_w_i_t_c_h
                    statements, as it is in the C language,  to  label
                    the code to be executed if none of the _c_a_s_e labels
                    matches the value switched on (3.7).

     DELETE         The  DELETE or RUBOUT key on the terminal normally
                    causes an interrupt to be sent to the current job.
                    Most users map the interrupt character to ^C.

     detached       A command that continues running in the _b_a_c_k_g_r_o_u_n_d
                    after you log out is said to be _d_e_t_a_c_h_e_d.

     diagnostic     An error message produced by a  program  is  often
                    referred  to as a _d_i_a_g_n_o_s_t_i_c.  Most error messages
                    are not written to the _s_t_a_n_d_a_r_d _o_u_t_p_u_t, since that
                    is often directed away  from  the  terminal  (1.3,
                    1.5).   Error  messages are instead written to the
                    _d_i_a_g_n_o_s_t_i_c _o_u_t_p_u_t which may be directed away  from
                    the  terminal,  but usually is not.  Thus _d_i_a_g_n_o_s_-
                    _t_i_c_s will usually appear on the terminal (2.5).

     directory      A structure which contains files.  At any time you
                    are in one particular _d_i_r_e_c_t_o_r_y whose name can  be
                    printed  by  the  command  _p_w_d.  The _c_h_d_i_r command
                    will change you to another _d_i_r_e_c_t_o_r_y, and make the
                    files in that _d_i_r_e_c_t_o_r_y visible. The _d_i_r_e_c_t_o_r_y  in
                    which  you  are when you first log in is your _h_o_m_e
                    directory (1.1, 2.7).

     directory stack
                    The shell saves the names of previous _w_o_r_k_i_n_g  _d_i_-
                    _r_e_c_t_o_r_i_e_s  in  the _d_i_r_e_c_t_o_r_y _s_t_a_c_k when you change
                    your current _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y via the _p_u_s_h_d  com-
                    mand.  The _d_i_r_e_c_t_o_r_y _s_t_a_c_k can be printed by using
                    the  _d_i_r_s  command,  which  includes  your current
                    _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y as the first directory  name  on
                    the left (2.7).

     dirs           The  _d_i_r_s  command  prints  the  shell's _d_i_r_e_c_t_o_r_y
                    _s_t_a_c_k (2.7).

     du             The _d_u command is a program (described  in  du(1))
                    which  prints the number of disk blocks is all di-
                    rectories below and including your current _w_o_r_k_i_n_g
                    _d_i_r_e_c_t_o_r_y (2.6).










     An Introduction to the C shell                           USD:4-53


     echo           The _e_c_h_o command prints its arguments (1.6, 3.6).

     else           The _e_l_s_e command is part of the  `if-then-else-en-
                    dif' control command construct (3.6).

     endif          If  an  _i_f  statement is ended with the word _t_h_e_n,
                    all lines following the _i_f up to a  line  starting
                    with  the  word  _e_n_d_i_f or _e_l_s_e are executed if the
                    condition between parentheses after the _i_f is true
                    (3.6).

     EOF            An _e_n_d-_o_f-_f_i_l_e is generated by the terminal  by  a
                    control-d, and whenever a command reads to the end
                    of  a file which it has been given as input.  Com-
                    mands receiving input from a _p_i_p_e receive an  _e_n_d-
                    _o_f-_f_i_l_e  when  the command sending them input com-
                    pletes.  Most commands terminate when they receive
                    an _e_n_d-_o_f-_f_i_l_e.  The shell has an option to ignore
                    _e_n_d-_o_f-_f_i_l_e from a terminal input which  may  help
                    you  keep  from logging out accidentally by typing
                    too many control-d's (1.1, 1.8, 3.8).

     escape         A character `\' used to prevent the special  mean-
                    ing of a metacharacter is said to _e_s_c_a_p_e the char-
                    acter from its special meaning.  Thus

                         echo \*

                    will echo the character `*' while just

                         echo *

                    will echo the names of the file in the current di-
                    rectory.   In  this  example, \ _e_s_c_a_p_es `*' (1.7).
                    There is also a non-printing character called  _e_s_-
                    _c_a_p_e,  usually labelled ESC or ALTMODE on terminal
                    keyboards.  Some older UNIX systems use this char-
                    acter to indicate that output is to be  _s_u_s_p_e_n_d_e_d.
                    Most  systems use control-s to stop the output and
                    control-q to start it.

     /etc/passwd    This file contains information about the  accounts
                    currently  on  the  system.  It consists of a line
                    for each account  with  fields  separated  by  `:'
                    characters  (1.8).   You  can look at this file by
                    saying

                         cat /etc/passwd

                    The commands _f_i_n_g_e_r and _g_r_e_p  are  often  used  to
                    search  for  information  in  this file.  See fin-
                    ger(1), passwd(5), and grep(1) for more details.











     USD:4-54                           An Introduction to the C shell


     exit           The _e_x_i_t command is used to force termination of a
                    shell script, and is built into the shell (3.9).

     exit status    A command which discovers a  problem  may  reflect
                    this  back  to the command (such as a shell) which
                    invoked (executed) it.  It does this by  returning
                    a  non-zero number as its _e_x_i_t _s_t_a_t_u_s, a status of
                    zero being considered `normal  termination'.   The
                    _e_x_i_t  command can be used to force a shell command
                    script to give a non-zero _e_x_i_t _s_t_a_t_u_s (3.6).

     expansion      The replacement of  strings  in  the  shell  input
                    which  contain  metacharacters by other strings is
                    referred to as the process of _e_x_p_a_n_s_i_o_n.  Thus the
                    replacement of the word `*' by a  sorted  list  of
                    files  in the current directory is a `filename ex-
                    pansion'.  Similarly the replacement of the  char-
                    acters  `!!'  by the text of the last command is a
                    `history expansion'.  _E_x_p_a_n_s_i_o_n_s are also referred
                    to as _s_u_b_s_t_i_t_u_t_i_o_n_s (1.6, 3.4, 4.2).

     expressions    _E_x_p_r_e_s_s_i_o_n_s are used in the shell to  control  the
                    conditional  structures  used  in  the  writing of
                    shell scripts and in calculating values for  these
                    scripts.  The operators available in shell _e_x_p_r_e_s_-
                    _s_i_o_n_s are those of the language C (3.5).

     extension      Filenames  often consist of a _b_a_s_e name and an _e_x_-
                    _t_e_n_s_i_o_n separated by the character `.'.   By  con-
                    vention,  groups  of related files often share the
                    same _r_o_o_t name.  Thus if `prog.c' were  a  C  pro-
                    gram,  then the object file for this program would
                    be stored in `prog.o'.  Similarly a paper  written
                    with the `-me' nroff macro package might be stored
                    in  `paper.me'  while  a formatted version of this
                    paper might be kept in `paper.out' and a  list  of
                    spelling errors in `paper.errs' (1.6).

     fg             The  _j_o_b _c_o_n_t_r_o_l command _f_g is used to run a _b_a_c_k_-
                    _g_r_o_u_n_d or _s_u_s_p_e_n_d_e_d job in  the  _f_o_r_e_g_r_o_u_n_d  (1.8,
                    2.6).

     filename       Each  file  in UNIX has a name consisting of up to
                    14 characters and not including the character  `/'
                    which  is  used  in _p_a_t_h_n_a_m_e building.  Most _f_i_l_e_-
                    _n_a_m_e_s do not begin with  the  character  `.',  and
                    contain only letters and digits with perhaps a `.'
                    separating  the  _b_a_s_e portion of the _f_i_l_e_n_a_m_e from
                    an _e_x_t_e_n_s_i_o_n (1.6).

     filename expansion
                    _F_i_l_e_n_a_m_e _e_x_p_a_n_s_i_o_n uses  the  metacharacters  `*',
                    `?' and `[' and `]' to provide a convenient mecha-
                    nism  for  naming files.  Using _f_i_l_e_n_a_m_e _e_x_p_a_n_s_i_o_n









     An Introduction to the C shell                           USD:4-55


                    it is easy to name all the files  in  the  current
                    directory,  or  all files which have a common _r_o_o_t
                    name. Other _f_i_l_e_n_a_m_e _e_x_p_a_n_s_i_o_n mechanisms use  the
                    metacharacter  `~' and allow files in other users'
                    directories to be named easily (1.6, 4.2).

     flag           Many UNIX commands accept arguments which are  not
                    the  names of files or other users but are used to
                    modify the action of the commands.  These are  re-
                    ferred  to as _f_l_a_g options, and by convention con-
                    sist of one or more letters preceded by the  char-
                    acter `-' (1.2).  Thus the _l_s (list files) command
                    has  an  option  `-s'  to list the sizes of files.
                    This is specified

                         ls -s

     foreach        The _f_o_r_e_a_c_h command is used in shell  scripts  and
                    at  the  terminal  to  specify repetition of a se-
                    quence of commands while the value  of  a  certain
                    shell  variable  ranges  through  a specified list
                    (3.6, 4.1).

     foreground     When commands are executing in the normal way such
                    that the shell is waiting for them to  finish  be-
                    fore  prompting  for another command they are said
                    to be _f_o_r_e_g_r_o_u_n_d _j_o_b_s  or  _r_u_n_n_i_n_g  _i_n  _t_h_e  _f_o_r_e_-
                    _g_r_o_u_n_d.   This is as opposed to _b_a_c_k_g_r_o_u_n_d.  _F_o_r_e_-
                    _g_r_o_u_n_d jobs can be stopped  by  signals  from  the
                    terminal  caused by typing different control char-
                    acters at the keyboard (1.8, 2.6).

     goto           The shell has a command _g_o_t_o used in shell scripts
                    to transfer control to a given label (3.7).

     grep           The _g_r_e_p command searches through a list of  argu-
                    ment files for a specified string.  Thus

                         grep bill /etc/passwd

                    will print each line in the file _/_e_t_c_/_p_a_s_s_w_d which
                    contains  the string `bill'.  Actually, _g_r_e_p scans
                    for _r_e_g_u_l_a_r _e_x_p_r_e_s_s_i_o_n_s in the sense of  the  edi-
                    tors  ed(1)  and ex(1).  _G_r_e_p stands for `globally
                    find _r_e_g_u_l_a_r _e_x_p_r_e_s_s_i_o_n and print' (2.4).

     head           The _h_e_a_d command prints the first few lines of one
                    or more files.  If you have a bunch of files  con-
                    taining  text  which you are wondering about it is
                    sometimes useful to run _h_e_a_d with these  files  as
                    arguments.   This will usually show enough of what
                    is in these files to let you decide which you  are
                    interested in (1.5).
                    _H_e_a_d  is  also  used  to  describe  the  part of a









     USD:4-56                           An Introduction to the C shell


                    _p_a_t_h_n_a_m_e before and including the last `/' charac-
                    ter.  The _t_a_i_l of a _p_a_t_h_n_a_m_e is the part after the
                    last `/'.  The `:h' and `:t' modifiers  allow  the
                    _h_e_a_d or _t_a_i_l of a _p_a_t_h_n_a_m_e stored in a shell vari-
                    able to be used (3.6).

     history        The _h_i_s_t_o_r_y mechanism of the shell allows previous
                    commands  to be repeated, possibly after modifica-
                    tion to correct typing mistakes or to  change  the
                    meaning  of  the command.  The shell has a _h_i_s_t_o_r_y
                    _l_i_s_t where these commands are kept, and a  _h_i_s_t_o_r_y
                    variable  which  controls  how  large this list is
                    (2.3).

     home directory
                    Each user has a _h_o_m_e _d_i_r_e_c_t_o_r_y, which is given  in
                    your  entry  in  the  password  file, _/_e_t_c_/_p_a_s_s_w_d.
                    This is the directory which you are placed in when
                    you first log in.  The _c_d or _c_h_d_i_r command with no
                    arguments takes you back to this directory,  whose
                    name  is recorded in the shell variable _h_o_m_e.  You
                    can also access  the  _h_o_m_e  _d_i_r_e_c_t_o_r_i_e_s  of  other
                    users in forming filenames using a _f_i_l_e_n_a_m_e _e_x_p_a_n_-
                    _s_i_o_n notation and the character `~' (1.6).

     if             A  conditional  command  within  the shell, the _i_f
                    command is used in shell command scripts  to  make
                    decisions about what course of action to take next
                    (3.6).

     ignoreeof      Normally,  your shell will exit, printing `logout'
                    if you type a control-d at a prompt of `% '.  This
                    is the way you usually log off  the  system.   You
                    can _s_e_t the _i_g_n_o_r_e_e_o_f variable if you wish in your
                    _._l_o_g_i_n file and then use the command _l_o_g_o_u_t to log
                    out.  This is useful if you sometimes accidentally
                    type  too many control-d characters, logging your-
                    self off (2.2).

     input          Many commands on UNIX take  information  from  the
                    terminal  or  from  files  which they then act on.
                    This information is called _i_n_p_u_t.   Commands  nor-
                    mally  read  _i_n_p_u_t from their _s_t_a_n_d_a_r_d _i_n_p_u_t which
                    is, by default, the terminal.  This _s_t_a_n_d_a_r_d _i_n_p_u_t
                    can be redirected from a file using a shell metan-
                    otation with the  character  `<'.   Many  commands
                    will  also  read from a file specified as an argu-
                    ment.  Commands placed in _p_i_p_e_l_i_n_e_s will read from
                    the  output  of  the  previous  command   in   the
                    _p_i_p_e_l_i_n_e.   The  leftmost  command  in  a _p_i_p_e_l_i_n_e
                    reads from the terminal if  you  neither  redirect
                    its  _i_n_p_u_t  nor give it a filename to use as _s_t_a_n_-
                    _d_a_r_d _i_n_p_u_t.  Special mechanisms exist for  supply-
                    ing input to commands in shell scripts (1.5, 3.8).









     An Introduction to the C shell                           USD:4-57


     interrupt      An _i_n_t_e_r_r_u_p_t is a signal to a program that is gen-
                    erated  by  typing  ^C. (On older versions of UNIX
                    the RUBOUT or DELETE key were used for  this  pur-
                    pose.)  It causes most programs to stop execution.
                    Certain  programs,  such as the shell and the edi-
                    tors, handle an _i_n_t_e_r_r_u_p_t in special ways, usually
                    by stopping what they are doing and prompting  for
                    another command.  While the shell is executing an-
                    other  command  and  waiting for it to finish, the
                    shell does not listen to  _i_n_t_e_r_r_u_p_t_s.   The  shell
                    often wakes up when you hit _i_n_t_e_r_r_u_p_t because many
                    commands  die when they receive an _i_n_t_e_r_r_u_p_t (1.8,
                    3.9).

     job            One or more commands typed on the same input  line
                    separated  by  `|'  or  `;' characters are run to-
                    gether and are called a _j_o_b.  Simple commands  run
                    by  themselves  without  any `|' or `;' characters
                    are the simplest _j_o_b_s.   _J_o_b_s  are  classified  as
                    _f_o_r_e_g_r_o_u_n_d, _b_a_c_k_g_r_o_u_n_d, or _s_u_s_p_e_n_d_e_d (2.6).

     job control    The  builtin  functions that control the execution
                    of jobs are called _j_o_b  _c_o_n_t_r_o_l  commands.   These
                    are _b_g, _f_g, _s_t_o_p, and _k_i_l_l (2.6).

     job number     When  each  job  is started it is assigned a small
                    number called a _j_o_b _n_u_m_b_e_r which is  printed  next
                    to  the  job  in  the  output of the _j_o_b_s command.
                    This number, preceded by a `%' character,  can  be
                    used as an argument to _j_o_b _c_o_n_t_r_o_l commands to in-
                    dicate a specific job (2.6).

     jobs           The  _j_o_b_s command prints a table showing jobs that
                    are either running in the _b_a_c_k_g_r_o_u_n_d or  are  _s_u_s_-
                    _p_e_n_d_e_d (2.6).

     kill           A command which sends a signal to a job causing it
                    to terminate (2.6).

     .login         The  file _._l_o_g_i_n in your _h_o_m_e directory is read by
                    the shell each time you log in  to  UNIX  and  the
                    commands  there  are executed.  There are a number
                    of commands which are usefully placed here,  espe-
                    cially _s_e_t commands to the shell itself (2.1).

     login shell    The  shell  that  is started on your terminal when
                    you log in is called your _l_o_g_i_n _s_h_e_l_l.  It is dif-
                    ferent from other shells which you may  run  (e.g.
                    on shell scripts) in that it reads the _._l_o_g_i_n file
                    before  reading  commands from the terminal and it
                    reads the _._l_o_g_o_u_t file after you log out (2.1).

     logout         The _l_o_g_o_u_t command causes a login shell  to  exit.
                    Normally,  a  login  shell  will exit when you hit









     USD:4-58                           An Introduction to the C shell


                    control-d generating an _e_n_d-_o_f-_f_i_l_e,  but  if  you
                    have  set  _i_g_n_o_r_e_e_o_f in your _._l_o_g_i_n file then this
                    will not work and you must use _l_o_g_o_u_t to  log  off
                    the UNIX system (2.8).

     .logout        When  you log off UNIX the shell will execute com-
                    mands from the file _._l_o_g_o_u_t in your _h_o_m_e directory
                    after it prints `logout'.

     lpr            The command _l_p_r is the line printer  daemon.   The
                    standard  input  of _l_p_r spooled and printed on the
                    UNIX line printer.  You can also give _l_p_r  a  list
                    of  filenames  as  arguments to be printed.  It is
                    most common to use _l_p_r as the last component of  a
                    _p_i_p_e_l_i_n_e (2.3).

     ls             The  _l_s  (list  files)  command is one of the most
                    commonly used UNIX  commands.   With  no  argument
                    filenames  it prints the names of the files in the
                    current directory.  It has a number of useful _f_l_a_g
                    arguments, and can also be given the names of  di-
                    rectories as arguments, in which case it lists the
                    names of the files in these directories (1.2).

     mail           The  _m_a_i_l program is used to send and receive mes-
                    sages from other UNIX users  (1.1,  2.1),  whether
                    they are logged on or not.

     make           The  _m_a_k_e  command is used to maintain one or more
                    related files and to organize functions to be per-
                    formed on these files.  In many ways _m_a_k_e is  eas-
                    ier  to  use,  and more helpful than shell command
                    scripts (3.2).

     makefile       The file containing commands for  _m_a_k_e  is  called
                    _m_a_k_e_f_i_l_e or _M_a_k_e_f_i_l_e (3.2).

     manual         The _m_a_n_u_a_l often referred to is the `UNIX manual'.
                    It contains 9 numbered sections with a description
                    of  each  UNIX  program  (section  1), system call
                    (section 2), subroutine (section 3), device  (sec-
                    tion  4), special data structure (section 5), game
                    (section 6), miscellaneous item (section 7),  sys-
                    tem administration program (section 8), and kernel
                    internals  (section 9).  There are also supplemen-
                    tary documents (tutorials  and  reference  guides)
                    for  individual programs which require explanation
                    in more detail.  An online version of  the  _m_a_n_u_a_l
                    is  accessible through the _m_a_n command.  Its docu-
                    mentation can be obtained online via

                         man man

                    If you can't decide what manual page to  look  in,









     An Introduction to the C shell                           USD:4-59


                    try  the  apropos(1)  command.   The supplementary
                    documents are in subdirectories of /usr/doc.

     metacharacter
                    Many characters which are neither letters nor dig-
                    its have special meaning either to the shell or to
                    UNIX.  These characters are called _m_e_t_a_c_h_a_r_a_c_t_e_r_s.
                    If it is necessary to place  these  characters  in
                    arguments  to  commands  without them having their
                    special meaning then they must be _q_u_o_t_e_d.  An  ex-
                    ample  of  a  _m_e_t_a_c_h_a_r_a_c_t_e_r  is the character `>',
                    which is used to indicate placement of output into
                    a file.  For the purposes of  the  _h_i_s_t_o_r_y  mecha-
                    nism,  most  unquoted _m_e_t_a_c_h_a_r_a_c_t_e_r_s form separate
                    words (1.4).  The appendix to this  user's  manual
                    lists  the _m_e_t_a_c_h_a_r_a_c_t_e_r_s in groups by their func-
                    tion.

     mkdir          The _m_k_d_i_r command is used to create a  new  direc-
                    tory.

     modifier       Substitutions with the _h_i_s_t_o_r_y mechanism, keyed by
                    the  character  `!'   or  of  variables  using the
                    metacharacter `$', are often subjected to  modifi-
                    cations,  indicated  by  placing the character `:'
                    after the substitution and following this with the
                    _m_o_d_i_f_i_e_r itself.  The _c_o_m_m_a_n_d _s_u_b_s_t_i_t_u_t_i_o_n  mecha-
                    nism can also be used to perform modification in a
                    similar  way,  but  this  notation  is  less clear
                    (3.6).

     more           The program _m_o_r_e writes a file  on  your  terminal
                    allowing you to control how much text is displayed
                    at a time.  _M_o_r_e can move through the file screen-
                    full  by screen-full, line by line, search forward
                    for a string, or start again at the  beginning  of
                    the  file.   It  is  generally  the easiest way of
                    viewing a file (1.8).

     noclobber      The shell has a variable _n_o_c_l_o_b_b_e_r  which  may  be
                    set  in  the file _._l_o_g_i_n to prevent accidental de-
                    struction of files by the `>'  output  redirection
                    metasyntax of the shell (2.2, 2.5).

     noglob         The  shell  variable _n_o_g_l_o_b is set to suppress the
                    _f_i_l_e_n_a_m_e _e_x_p_a_n_s_i_o_n  of  arguments  containing  the
                    metacharacters `~', `*', `?', `[' and `]' (3.6).

     notify         The  _n_o_t_i_f_y  command  tells the shell to report on
                    the termination of a specific  _b_a_c_k_g_r_o_u_n_d  _j_o_b  at
                    the exact time it occurs as opposed to waiting un-
                    til just before the next prompt to report the ter-
                    mination.  The _n_o_t_i_f_y variable, if set, causes the
                    shell   to   always   report  the  termination  of









     USD:4-60                           An Introduction to the C shell


                    _b_a_c_k_g_r_o_u_n_d jobs exactly when they occur (2.6).

     onintr         The _o_n_i_n_t_r command is built into the shell and  is
                    used  to  control  the  action  of a shell command
                    script when an _i_n_t_e_r_r_u_p_t signal is received (3.9).

     output         Many commands in UNIX result in some lines of text
                    which are called their  _o_u_t_p_u_t.   This  _o_u_t_p_u_t  is
                    usually  placed  on  what is known as the _s_t_a_n_d_a_r_d
                    _o_u_t_p_u_t, which is normally connected to the  user's
                    terminal.   The  shell  has  a  syntax  using  the
                    metacharacter `>'  for  redirecting  the  _s_t_a_n_d_a_r_d
                    _o_u_t_p_u_t  of  a  command to a file (1.3).  Using the
                    _p_i_p_e mechanism and the  metacharacter  `|'  it  is
                    also  possible for the _s_t_a_n_d_a_r_d _o_u_t_p_u_t of one com-
                    mand to become the _s_t_a_n_d_a_r_d _i_n_p_u_t of another  com-
                    mand  (1.5).   Certain  commands  such as the line
                    printer daemon _l_p_d do not place their  results  on
                    the  _s_t_a_n_d_a_r_d  _o_u_t_p_u_t  but  rather  in more useful
                    places such as on the line printer  (2.3).   Simi-
                    larly  the  _w_r_i_t_e command places its output on an-
                    other user's terminal  rather  than  its  _s_t_a_n_d_a_r_d
                    _o_u_t_p_u_t  (2.3).   Commands  also  have a _d_i_a_g_n_o_s_t_i_c
                    _o_u_t_p_u_t where  they  write  their  error  messages.
                    Normally  these  go  to  the  terminal even if the
                    _s_t_a_n_d_a_r_d _o_u_t_p_u_t has been sent to a file or another
                    command, but it is possible to direct error  diag-
                    nostics along with _s_t_a_n_d_a_r_d _o_u_t_p_u_t using a special
                    metanotation (2.5).

     path           The  shell  has  a  variable  _p_a_t_h which gives the
                    names of the directories in which it searches  for
                    the  commands which it is given.  It always checks
                    first to see if the command it is given  is  built
                    into the shell.  If it is, then it need not search
                    for  the  command  as it can do it internally.  If
                    the  command  is  not  builtin,  then  the   shell
                    searches for a file with the name given in each of
                    the  directories  in  the  _p_a_t_h  variable, left to
                    right.  Since the normal definition  of  the  _p_a_t_h
                    variable is

                         path (/bin /usr/bin .)

                    the  shell  normally  looks in the standard system
                    directories `/bin' and `/usr/bin',  and  then  the
                    current  directory,  for  the named command (2.2).
                    If the command cannot  be  found  the  shell  will
                    print  an error diagnostic.  Scripts of shell com-
                    mands will be executed using another shell to  in-
                    terpret  them  if  they  have `execute' permission
                    set.  This is normally true because a  command  of











     An Introduction to the C shell                           USD:4-61


                    the form

                         chmod 755 script

                    was  executed  to  turn this execute permission on
                    (3.3).  If you add new commands to a directory  in
                    the  _p_a_t_h,  you  should  issue  the command _r_e_h_a_s_h
                    (2.2).

     pathname       A list of  names,  separated  by  `/'  characters,
                    forms a _p_a_t_h_n_a_m_e.  Each _c_o_m_p_o_n_e_n_t, between succes-
                    sive  `/'  characters,  names a directory in which
                    the next _c_o_m_p_o_n_e_n_t file resides.  _P_a_t_h_n_a_m_e_s  which
                    begin with the character `/' are interpreted rela-
                    tive  to  the  _r_o_o_t  directory  in the filesystem.
                    Other _p_a_t_h_n_a_m_e_s are interpreted  relative  to  the
                    current  directory  as  reported by _p_w_d.  The last
                    component of a _p_a_t_h_n_a_m_e may name a directory,  but
                    usually names a file.

     pipeline       A  group of commands which are connected together,
                    the _s_t_a_n_d_a_r_d _o_u_t_p_u_t of each connected to the _s_t_a_n_-
                    _d_a_r_d _i_n_p_u_t of the next, is called a _p_i_p_e_l_i_n_e.  The
                    _p_i_p_e mechanism used to connect these  commands  is
                    indicated  by  the  shell  metacharacter `|' (1.5,
                    2.3).

     popd           The _p_o_p_d command changes the shell's  _w_o_r_k_i_n_g  _d_i_-
                    _r_e_c_t_o_r_y  to  the  directory you most recently left
                    using the _p_u_s_h_d command.  It returns to the direc-
                    tory without having to type its  name,  forgetting
                    the  name  of the current _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y before
                    doing so (2.7).

     port           The part of a computer system to which each termi-
                    nal is connected is called a  _p_o_r_t.   Usually  the
                    system  has a fixed number of _p_o_r_t_s, some of which
                    are connected to telephone lines for  dial-up  ac-
                    cess,  and some of which are permanently wired di-
                    rectly to specific terminals.

     pr             The _p_r command is used to prepare listings of  the
                    contents  of files with headers giving the name of
                    the file and the date and time at which  the  file
                    was last modified (2.3).

     printenv       The  _p_r_i_n_t_e_n_v command is used to print the current
                    setting of variables in the environment (2.8).

     process        An instance of  a  running  program  is  called  a
                    _p_r_o_c_e_s_s (2.6).  UNIX assigns each _p_r_o_c_e_s_s a unique
                    number  when  it  is  started - called the _p_r_o_c_e_s_s
                    _n_u_m_b_e_r.  _P_r_o_c_e_s_s _n_u_m_b_e_r_s can be used to stop indi-
                    vidual _p_r_o_c_e_s_s_e_s using the _k_i_l_l or  _s_t_o_p  commands









     USD:4-62                           An Introduction to the C shell


                    when  the  _p_r_o_c_e_s_s_e_s  are part of a detached _b_a_c_k_-
                    _g_r_o_u_n_d job.

     program        Usually synonymous with _c_o_m_m_a_n_d; a binary file  or
                    shell command script which performs a useful func-
                    tion is often called a _p_r_o_g_r_a_m.

     prompt         Many  programs will print a _p_r_o_m_p_t on the terminal
                    when they expect input.   Thus  the  editor  ex(1)
                    will print a `:' when it expects input.  The shell
                    _p_r_o_m_p_t_s  for input with `% ' and occasionally with
                    `? '  when  reading  commands  from  the  terminal
                    (1.1).   The shell has a variable _p_r_o_m_p_t which may
                    be set to a different value to change the  shell's
                    main  _p_r_o_m_p_t.   This is mostly used when debugging
                    the shell (2.8).

     pushd          The _p_u_s_h_d command, which means  `push  directory',
                    changes the shell's _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y and also re-
                    members  the  current _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y before the
                    change is made, allowing you to return to the same
                    directory via the _p_o_p_d command later  without  re-
                    typing its name (2.7).

     ps             The  _p_s  command is used to show the processes you
                    are currently running.  Each process is shown with
                    its unique process number, an  indication  of  the
                    terminal  name it is attached to, an indication of
                    the state of the process (whether it  is  running,
                    stopped,   awaiting  some  event  (sleeping),  and
                    whether it is swapped out), and the amount of  CPU
                    time  it  has used so far.  The command is identi-
                    fied by printing some of the words  used  when  it
                    was invoked (2.6).

     pwd            The  _p_w_d  command  prints the full _p_a_t_h_n_a_m_e of the
                    current _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y.  The _d_i_r_s builtin  com-
                    mand is usually a better and faster choice.

     quit           The _q_u_i_t signal, generated by a control-\, is used
                    to terminate programs which are behaving unreason-
                    ably.   It  normally  produces  a  core image file
                    (1.8).

     quotation      The process by which metacharacters are  prevented
                    their  special meaning, usually by using the char-
                    acter `'' in pairs, or by using the character `\',
                    is referred to as _q_u_o_t_a_t_i_o_n (1.7).

     redirection    The routing of input or output from or to  a  file
                    is known as _r_e_d_i_r_e_c_t_i_o_n of input or output (1.3).

     rehash         The  _r_e_h_a_s_h command tells the shell to rebuild its
                    internal table of  which  commands  are  found  in









     An Introduction to the C shell                           USD:4-63


                    which directories in your _p_a_t_h.  This is necessary
                    when  a  new  program is installed in one of these
                    directories (2.8).

     relative pathname
                    A _p_a_t_h_n_a_m_e which does not  begin  with  a  `/'  is
                    called a _r_e_l_a_t_i_v_e _p_a_t_h_n_a_m_e since it is interpreted
                    _r_e_l_a_t_i_v_e  to  the  current _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y.  The
                    first _c_o_m_p_o_n_e_n_t of such a _p_a_t_h_n_a_m_e refers to  some
                    file  or  directory  in the _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y, and
                    subsequent _c_o_m_p_o_n_e_n_t_s between `/' characters refer
                    to directories below the _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y.  _P_a_t_h_-
                    _n_a_m_e_s that are not _r_e_l_a_t_i_v_e  are  called  _a_b_s_o_l_u_t_e
                    _p_a_t_h_n_a_m_e_s (1.6).

     repeat         The  _r_e_p_e_a_t  command  iterates  another  command a
                    specified number of times.

     root           The directory that is at the top of the entire di-
                    rectory structure is  called  the  _r_o_o_t  directory
                    since  it  is the `root' of the entire tree struc-
                    ture of directories.  The name used  in  _p_a_t_h_n_a_m_e_s
                    to  indicate  the _r_o_o_t is `/'.  _P_a_t_h_n_a_m_e_s starting
                    with `/' are said to be _a_b_s_o_l_u_t_e since they  start
                    at  the  _r_o_o_t directory.  _R_o_o_t is also used as the
                    part of a _p_a_t_h_n_a_m_e that is left after removing the
                    _e_x_t_e_n_s_i_o_n.  See _f_i_l_e_n_a_m_e for a further explanation
                    (1.6).

     RUBOUT         The RUBOUT or DELETE key is often  used  to  erase
                    the  previously typed character; some users prefer
                    the BACKSPACE for this purpose.  On older versions
                    of UNIX this key served as the INTR character.

     scratch file   Files whose names begin with a `#' are referred to
                    as _s_c_r_a_t_c_h _f_i_l_e_s, since they are automatically re-
                    moved by the system after a couple of days of non-
                    use, or more  frequently  if  disk  space  becomes
                    tight (1.3).

     script         Sequences  of  shell commands placed in a file are
                    called shell command _s_c_r_i_p_t_s.  It is often  possi-
                    ble  to  perform  simple tasks using these _s_c_r_i_p_t_s
                    without writing a program in a language such as C,
                    by using the shell to selectively run  other  pro-
                    grams (3.3, 3.10).

     set            The builtin _s_e_t command is used to assign new val-
                    ues  to  shell variables and to show the values of
                    the current variables.  Many shell variables  have
                    special  meaning to the shell itself.  Thus by us-
                    ing the _s_e_t command the behavior of the shell  can
                    be affected (2.1).










     USD:4-64                           An Introduction to the C shell


     setenv         Variables  in  the  environment  environ(5) can be
                    changed by using the _s_e_t_e_n_v builtin command (2.8).
                    The _p_r_i_n_t_e_n_v command can  be  used  to  print  the
                    value of the variables in the environment.

     shell          A  _s_h_e_l_l is a command language interpreter.  It is
                    possible to write  and  run  your  own  _s_h_e_l_l,  as
                    _s_h_e_l_l_s are no different than any other programs as
                    far as the system is concerned.  This manual deals
                    with  the  details of one particular _s_h_e_l_l, called
                    _c_s_h.

     shell script   See _s_c_r_i_p_t (3.3, 3.10).

     signal         A _s_i_g_n_a_l in UNIX is a short message that  is  sent
                    to  a  running  program  which causes something to
                    happen to that process.  _S_i_g_n_a_l_s are  sent  either
                    by  typing  special _c_o_n_t_r_o_l characters on the key-
                    board or by using the _k_i_l_l or _s_t_o_p commands  (1.8,
                    2.6).

     sort           The _s_o_r_t program sorts a sequence of lines in ways
                    that can be controlled by argument _f_l_a_g_s (1.5).

     source         The  _s_o_u_r_c_e  command causes the shell to read com-
                    mands from a specified file.  It  is  most  useful
                    for  reading  files  such as _._c_s_h_r_c after changing
                    them (2.8).

     special character
                    See _m_e_t_a_c_h_a_r_a_c_t_e_r_s and the appendix to  this  man-
                    ual.

     standard       We  refer often to the _s_t_a_n_d_a_r_d _i_n_p_u_t and _s_t_a_n_d_a_r_d
                    _o_u_t_p_u_t of commands.  See _i_n_p_u_t  and  _o_u_t_p_u_t  (1.3,
                    3.8).

     status         A  command  normally returns a _s_t_a_t_u_s when it fin-
                    ishes.  By convention a _s_t_a_t_u_s of  zero  indicates
                    that  the  command succeeded.  Commands may return
                    non-zero _s_t_a_t_u_s to  indicate  that  some  abnormal
                    event  has occurred.  The shell variable _s_t_a_t_u_s is
                    set to the _s_t_a_t_u_s returned by  the  last  command.
                    It is most useful in shell command scripts (3.6).

     stop           The _s_t_o_p command causes a _b_a_c_k_g_r_o_u_n_d job to become
                    _s_u_s_p_e_n_d_e_d (2.6).

     string         A sequential group of characters taken together is
                    called  a  _s_t_r_i_n_g.  _S_t_r_i_n_g_s can contain any print-
                    able characters (2.2).

     stty           The _s_t_t_y program changes certain parameters inside
                    UNIX which determine how your terminal is handled.









     An Introduction to the C shell                           USD:4-65


                    See stty(1) for a complete description (2.6).

     substitution   The shell implements  a  number  of  _s_u_b_s_t_i_t_u_t_i_o_n_s
                    where  sequences  indicated  by metacharacters are
                    replaced by other sequences.  Notable examples  of
                    this   are   history  _s_u_b_s_t_i_t_u_t_i_o_n  keyed  by  the
                    metacharacter `!' and variable _s_u_b_s_t_i_t_u_t_i_o_n  indi-
                    cated  by  `$'.  We also refer to _s_u_b_s_t_i_t_u_t_i_o_n_s as
                    _e_x_p_a_n_s_i_o_n_s (3.4).

     suspended      A job becomes _s_u_s_p_e_n_d_e_d after  a  STOP  signal  is
                    sent  to  it,  either by typing a _c_o_n_t_r_o_l-z at the
                    terminal (for _f_o_r_e_g_r_o_u_n_d jobs)  or  by  using  the
                    _s_t_o_p  command  (for  _b_a_c_k_g_r_o_u_n_d  jobs).  When _s_u_s_-
                    _p_e_n_d_e_d, a job temporarily stops running  until  it
                    is restarted by either the _f_g or _b_g command (2.6).

     switch         The  _s_w_i_t_c_h  command of the shell allows the shell
                    to select one of a number of sequences of commands
                    based on an argument string.  It is similar to the
                    _s_w_i_t_c_h statement in the language C (3.7).

     termination    When a command which is being executed finishes we
                    say it undergoes _t_e_r_m_i_n_a_t_i_o_n or _t_e_r_m_i_n_a_t_e_s.   Com-
                    mands normally terminate when they read an _e_n_d-_o_f-
                    _f_i_l_e from their _s_t_a_n_d_a_r_d _i_n_p_u_t.  It is also possi-
                    ble  to  terminate commands by sending them an _i_n_-
                    _t_e_r_r_u_p_t or _q_u_i_t signal (1.8).   The  _k_i_l_l  program
                    terminates specified jobs (2.6).

     then           The  _t_h_e_n command is part of the shell's `if-then-
                    else-endif'  control  construct  used  in  command
                    scripts (3.6).

     time           The _t_i_m_e command can be used to measure the amount
                    of  CPU and real time consumed by a specified com-
                    mand as well as the amount  of  disk  I/O,  memory
                    utilized,  and  number  of  page  faults and swaps
                    taken by the command (2.1, 2.8).

     tset           The _t_s_e_t program is used to set standard erase and
                    kill characters and to tell the system  what  kind
                    of terminal you are using.  It is often invoked in
                    a _._l_o_g_i_n file (2.1).

     tty            The  word  _t_t_y  is  a  historical abbreviation for
                    `teletype' which is frequently used in UNIX to in-
                    dicate the _p_o_r_t to which a given terminal is  con-
                    nected.   The  _t_t_y  command will print the name of
                    the  _t_t_y  or  _p_o_r_t  to  which  your  terminal   is
                    presently connected.

     unalias        The _u_n_a_l_i_a_s command removes aliases (2.8).










     USD:4-66                           An Introduction to the C shell


     UNIX           UNIX  is  an  operating  system on which _c_s_h runs.
                    UNIX provides facilities which allow _c_s_h to invoke
                    other programs such as editors and text formatters
                    which you may wish to use.

     unset          The _u_n_s_e_t command removes the definitions of shell
                    variables (2.2, 2.8).

     variable expansion
                    See _v_a_r_i_a_b_l_e_s and _e_x_p_a_n_s_i_o_n (2.2, 3.4).

     variables      _V_a_r_i_a_b_l_e_s in _c_s_h  hold  one  or  more  strings  as
                    value.   The  most  common  use of _v_a_r_i_a_b_l_e_s is in
                    controlling the behavior of the shell.  See  _p_a_t_h,
                    _n_o_c_l_o_b_b_e_r,  and _i_g_n_o_r_e_e_o_f for examples.  _V_a_r_i_a_b_l_e_s
                    such as _a_r_g_v are also used in writing  shell  pro-
                    grams (shell command scripts) (2.2).

     verbose        The  _v_e_r_b_o_s_e  shell  variable  can be set to cause
                    commands to be echoed after they are  history  ex-
                    panded.   This  is often useful in debugging shell
                    scripts.  The  _v_e_r_b_o_s_e  variable  is  set  by  the
                    shell's --vv command line option (3.10).

     wc             The  _w_c  program  calculates the number of charac-
                    ters, words, and lines in the  files  whose  names
                    are given as arguments (2.6).

     while          The  _w_h_i_l_e  builtin  control  construct is used in
                    shell command scripts (3.7).

     word           A sequence of characters which forms  an  argument
                    to  a  command  is called a _w_o_r_d.  Many characters
                    which are neither letters, digits,  `-',  `.'  nor
                    `/'  form _w_o_r_d_s all by themselves even if they are
                    not surrounded by blanks.  Any sequence of charac-
                    ters may be made into a  _w_o_r_d  by  surrounding  it
                    with  `'' characters except for the characters `''
                    and `!' which  require  special  treatment  (1.1).
                    This  process  of  placing  special  characters in
                    _w_o_r_d_s without  their  special  meaning  is  called
                    _q_u_o_t_i_n_g.

     working directory
                    At any given time you are in one particular direc-
                    tory,  called your _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_y.  This direc-
                    tory's name is printed by the _p_w_d command and  the
                    files listed by _l_s are the ones in this directory.
                    You can change _w_o_r_k_i_n_g _d_i_r_e_c_t_o_r_i_e_s using _c_h_d_i_r.

     write          The  _w_r_i_t_e  command is an obsolete way of communi-
                    cating with other users who are logged in to  UNIX
                    (you  have to take turns typing).  If you are both
                    using display terminals,  use  talk(1),  which  is









     An Introduction to the C shell                           USD:4-67


                    much more pleasant.



























































