Initial import from FreeBSD RELENG_4:
[dragonfly.git] / bin / csh / USD.doc / csh.1
1 .\" Copyright (c) 1980, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\"    must display the following acknowledgement:
14 .\"     This product includes software developed by the University of
15 .\"     California, Berkeley and its contributors.
16 .\" 4. Neither the name of the University nor the names of its contributors
17 .\"    may be used to endorse or promote products derived from this software
18 .\"    without specific prior written permission.
19 .\"
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 .\" SUCH DAMAGE.
31 .\"
32 .\"     @(#)csh.1       8.1 (Berkeley) 6/8/93
33 .\" $FreeBSD: src/bin/csh/USD.doc/csh.1,v 1.9.2.2 2003/01/24 02:12:02 keramida Exp $
34 .\"
35 .EH 'USD:4-%''An Introduction to the C shell'
36 .OH 'An Introduction to the C shell''USD:4-%'
37 .\".RP
38 .TL
39 An Introduction to the C shell
40 .AU
41 William Joy
42 (revised for 4.3BSD by Mark Seiden)
43 .AI
44 Computer Science Division
45 .br
46 Department of Electrical Engineering and Computer Science
47 .br
48 University of California, Berkeley
49 .br
50 Berkeley, California 94720
51 .AB
52 .I Csh
53 is a new command language interpreter for
54 .UX
55 systems.
56 It incorporates good features of other shells and a
57 .I history
58 mechanism similar to the
59 .I redo
60 of \s-2INTERLISP\s0.
61 While incorporating many features of other shells which make
62 writing shell programs (shell scripts) easier,
63 most of the features unique to
64 .I csh
65 are designed more for the interactive \s-2UNIX\s0 user.
66 .PP
67 \s-2UNIX\s0
68 users who have read a general introduction to the system
69 will find a valuable basic explanation of the shell here.
70 Simple terminal interaction with
71 .I csh
72 is possible after reading just the first section of this document.
73 The second section describes the shell's capabilities which you can
74 explore after you have begun to become acquainted with the shell.
75 Later sections introduce features which are useful, but not necessary
76 for all users of the shell.
77 .PP
78 Additional information includes an appendix listing special characters of the shell
79 and a glossary of terms and commands introduced in this manual.
80 .AE
81 .SH
82 .if n .ND
83 Introduction
84 .PP
85 A
86 .I shell
87 is a command language interpreter.
88 .I Csh
89 is the name of one particular command interpreter on
90 \s-2UNIX\s0.
91 The primary purpose of
92 .I csh
93 is to translate command lines typed at a terminal into
94 system actions, such as invocation of other programs.
95 .I Csh
96 is a user program just like any you might write.
97 Hopefully,
98 .I csh
99 will be a very useful program for you
100 in interacting with the \s-2UNIX\s0 system.
101 .PP
102 In addition to this document, you will want to refer to a copy
103 of the \s-2UNIX\s0 User Reference Manual.
104 The
105 .I csh
106 documentation in section 1 of the manual provides a full description of all
107 features of the shell and is the definitive reference for questions
108 about the shell.
109 .PP
110 Many words in this document are shown in
111 .I italics.
112 These are important words;
113 names of commands, and words which have special meaning in discussing
114 the shell and \s-2UNIX\s0.
115 Many of the words are defined in a glossary at the end of this document.
116 If you don't know what is meant by a word, you should look
117 for it in the glossary.
118 .SH
119 Acknowledgements
120 .PP
121 Numerous people have provided good input about previous versions
122 of
123 .I csh
124 and aided in its debugging and in the debugging of its documentation.
125 I would especially like to thank Michael Ubell
126 who made the crucial observation that history commands could be
127 done well over the word structure of input text, and implemented
128 a prototype history mechanism in an older version of the shell.
129 Eric Allman has also provided a large number of useful comments on the
130 shell, helping to unify those concepts which are present and to identify
131 and eliminate useless and marginally useful features.
132 Mike O'Brien suggested the pathname hashing
133 mechanism which speeds command execution.
134 Jim Kulp added the job control and directory stack primitives and
135 added their documentation to this introduction.
136 .br
137 .bp
138 .NH
139 Terminal usage of the shell
140 .NH 2
141 The basic notion of commands
142 .PP
143 A
144 .I shell
145 in
146 \s-2UNIX\s0
147 acts mostly as a medium through which other
148 .I programs
149 are invoked.
150 While it has a set of
151 .I builtin
152 functions which it performs directly,
153 most commands cause execution of programs that are, in fact,
154 external to the shell.
155 The shell is thus distinguished from the command interpreters of other
156 systems both by the fact that it is just a user program, and by the fact
157 that it is used almost exclusively as a mechanism for invoking other programs.
158 .PP
159 .I Commands
160 in the \s-2UNIX\s0 system consist of a list of strings or
161 .I words
162 interpreted as a
163 .I "command name"
164 followed by
165 .I arguments.
166 Thus the command
167 .DS
168 mail bill
169 .DE
170 consists of two words.
171 The first word
172 .I mail
173 names the command to be executed, in this case the
174 mail program which sends messages to other users.
175 The shell uses the name of the command in attempting to execute it for you.
176 It will look in a number of
177 .I directories
178 for a file with the name
179 .I mail
180 which is expected to contain the mail program.
181 .PP
182 The rest of the words of the command are given as
183 .I arguments
184 to the command itself when it is executed.
185 In this case we specified also the argument
186 .I bill
187 which is interpreted by the
188 .I mail
189 program to be the name of a user to whom mail is to be sent.
190 In normal terminal usage we might use the
191 .I mail
192 command as follows.
193 .DS
194 % mail bill
195 I have a question about the csh documentation.
196 My document seems to be missing page 5.
197 Does a page five exist?
198         Bill
199 EOT
200 %
201 .DE
202 .PP
203 Here we typed a message to send to
204 .I bill
205 and ended this message with a ^D which sent an end-of-file to
206 the mail program.
207 (Here and throughout this document, the notation ``^\fIx\fR''
208 is to be read ``control-\fIx\fR'' and represents the striking of the \fIx\fR
209 key while the control key is held down.)
210 The mail program
211 then echoed the characters `EOT' and transmitted our message.
212 The characters `% ' were printed before and after the mail command
213 by the shell to indicate that input was needed.
214 .PP
215 After typing the `% ' prompt the shell was reading command input from
216 our terminal.
217 We typed a complete command `mail bill'.
218 The shell then executed the
219 .I mail
220 program with argument
221 .I bill
222 and went dormant waiting for it to complete.
223 The mail program then read input from our terminal until we signalled
224 an end-of-file via typing a ^D after which the shell noticed
225 that mail had completed
226 and signaled us that it was ready to read from the terminal again by
227 printing another `% ' prompt.
228 .PP
229 This is the essential pattern of all interaction with \s-2UNIX\s0
230 through the shell.
231 A complete command is typed at the terminal, the shell executes
232 the command and when this execution completes, it prompts for a new command.
233 If you run the editor for an hour, the shell will patiently wait for
234 you to finish editing and obediently prompt you again whenever you finish
235 editing.
236 .PP
237 An example of a useful command you can execute now is the
238 .I tset
239 command, which sets the default
240 .I erase
241 and
242 .I kill
243 characters on your terminal \- the erase character erases the last
244 character you typed and the kill character erases the entire line you
245 have entered so far.
246 By default, the erase character is the delete key (equivalent to `^?')
247 and the kill character is `^U'.  Some people prefer to make the erase character
248 the backspace key (equivalent to `^H').
249 You can make this be true by typing
250 .DS
251 tset \-e
252 .DE
253 which tells the program
254 .I tset
255 to set the erase character to tset's default setting for this character
256 (a backspace).
257 .NH 2
258 Flag arguments
259 .PP
260 A useful notion in \s-2UNIX\s0 is that of a
261 .I flag
262 argument.
263 While many arguments to commands specify file names or user names,
264 some arguments rather specify an optional capability of the command
265 which you wish to invoke.
266 By convention, such arguments begin with the character `\-' (hyphen).
267 Thus the command
268 .DS
269 ls
270 .DE
271 will produce a list of the files in the current
272 .I "working directory" .
273 The option
274 .I \-s
275 is the size option, and
276 .DS
277 ls \-s
278 .DE
279 causes
280 .I ls
281 to also give, for each file the size of the file in blocks of 512
282 characters.
283 The manual section for each command in the \s-2UNIX\s0 reference manual
284 gives the available options for each command.
285 The
286 .I ls
287 command has a large number of useful and interesting options.
288 Most other commands have either no options or only one or two options.
289 It is hard to remember options of commands which are not used very
290 frequently, so most \s-2UNIX\s0 utilities perform only one or two functions
291 rather than having a large number of hard to remember options.
292 .NH 2
293 Output to files
294 .PP
295 Commands that normally read input or write output on the terminal
296 can also be executed with this input and/or output done to
297 a file.
298 .PP
299 Thus suppose we wish to save the current date in a file called `now'.
300 The command
301 .DS
302 date
303 .DE
304 will print the current date on our terminal.
305 This is because our terminal is the default
306 .I "standard output"
307 for the date command and the date command prints the date on its
308 standard output.
309 The shell lets us
310 .I redirect
311 the
312 .I "standard output"
313 of a command through a
314 notation using the
315 .I metacharacter
316 `>' and the name of the file where output is to be placed.
317 Thus the command
318 .DS
319 date > now
320 .DE
321 runs the
322 .I date
323 command such that its standard output is
324 the file `now' rather than the terminal.
325 Thus this command places the current date and time into the file `now'.
326 It is important to know that the
327 .I date
328 command was unaware that its output was going to a file rather than
329 to the terminal.
330 The shell performed this
331 .I redirection
332 before the command began executing.
333 .PP
334 One other thing to note here is that the file `now'
335 need not have existed before the
336 .I date
337 command was executed; the shell would have created the file if it did
338 not exist.
339 And if the file did exist?
340 If it had existed previously these previous contents would have been discarded!
341 A shell option
342 .I noclobber
343 exists to prevent this from happening accidentally;
344 it is discussed in section 2.2.
345 .PP
346 The system normally keeps files which you create with `>' and all other files.
347 Thus the default is for files to be permanent.  If you wish to create a file
348 which will be removed automatically, you can begin its name with a `#'
349 character, this `scratch' character denotes the fact that the file will
350 be a scratch file.*
351 .FS
352 *Note that if your erase character is a `#', you will have to precede the
353 `#' with a `\e'.  The fact that the `#' character is the old (pre-\s-2CRT\s0)
354 standard erase character means that it seldom appears in a file name, and
355 allows this convention to be used for scratch files.  If you are using a
356 \s-2CRT\s0, your erase character should be a ^H, as we demonstrated
357 in section 1.1 how this could be set up.
358 .FE
359 The system will remove such files after a couple of days,
360 or sooner if file space becomes very tight.
361 Thus, in running the
362 .I date
363 command above, we don't really want to save the output forever, so we
364 would more likely do
365 .DS
366 date > #now
367 .DE
368 .NH 2
369 Metacharacters in the shell
370 .PP
371 The shell has a large number of
372 special characters (like `>')
373 which indicate special functions.
374 We say that these notations have
375 .I syntactic
376 and
377 .I semantic
378 meaning to the shell.
379 In general, most characters which are neither letters nor digits
380 have special meaning to the shell.
381 We shall shortly learn a means of
382 .I quotation
383 which allows us to use
384 .I metacharacters
385 without the shell treating them in any special way.
386 .PP
387 Metacharacters normally have effect only when the shell is reading
388 our input.
389 We need not worry about placing shell metacharacters in a letter
390 we are sending via
391 .I mail,
392 or when we are typing in text or data to some other program.
393 Note that the shell is only reading input when it has prompted with
394 `% ' (although we can type our input even before it prompts).
395 .NH 2
396 Input from files; pipelines
397 .PP
398 We learned above how to
399 .I redirect
400 the
401 .I "standard output"
402 of a command
403 to a file.
404 It is also possible to redirect the
405 .I "standard input"
406 of a command from a file.
407 This is not often necessary since most commands will read from
408 a file whose name is given as an argument.
409 We can give the command
410 .DS
411 sort < data
412 .DE
413 to run the
414 .I sort
415 command with standard input, where the command normally
416 reads its input, from the file
417 `data'.
418 We would more likely say
419 .DS
420 sort data
421 .DE
422 letting the
423 .I sort
424 command open the file
425 `data'
426 for input itself since this is less to type.
427 .PP
428 We should note that if we just typed
429 .DS
430 sort
431 .DE
432 then the sort program would sort lines from its
433 .I "standard input."
434 Since we did not
435 .I redirect
436 the standard input, it would sort lines as we typed them on the terminal
437 until we typed a ^D to indicate an end-of-file.
438 .PP
439 A most useful capability is the ability to combine the standard output
440 of one command with the standard input of another, i.e. to run the
441 commands in a sequence known as a
442 .I pipeline.
443 For instance the command
444 .DS
445 ls \-s
446 .DE
447 normally produces a list of the files in our directory with the size
448 of each in blocks of 512 characters.
449 If we are interested in learning which of our files is largest we
450 may wish to have this sorted by size rather than by name, which is
451 the default way in which
452 .I ls
453 sorts.
454 We could look at the many options of
455 .I ls
456 to see if there was an option to do this but would eventually discover
457 that there is not.
458 Instead we can use a couple of simple options of the
459 .I sort
460 command, combining it with
461 .I ls
462 to get what we want.
463 .PP
464 The
465 .I \-n
466 option of sort specifies a numeric sort rather than an alphabetic sort.
467 Thus
468 .DS
469 ls \-s | sort \-n
470 .DE
471 specifies that the output of the
472 .I ls
473 command run with the option
474 .I \-s
475 is to be
476 .I piped
477 to the command
478 .I sort
479 run with the numeric sort option.
480 This would give us a sorted list of our files by size, but with the
481 smallest first.
482 We could then use the
483 .I \-r
484 reverse sort option and the
485 .I head
486 command in combination with the previous command doing
487 .DS
488 ls \-s | sort \-n \-r | head \-5
489 .DE
490 Here we have taken a list of our files sorted alphabetically,
491 each with the size in blocks.
492 We have run this to the standard input of the
493 .I sort
494 command asking it to sort numerically in reverse order (largest first).
495 This output has then been run into the command
496 .I head
497 which gives us the first few lines.
498 In this case we have asked
499 .I head
500 for the first 5 lines.
501 Thus this command gives us the names and sizes of our 5 largest files.
502 .PP
503 The notation introduced above is called the
504 .I pipe
505 mechanism.
506 Commands separated by `\||\|' characters are connected together by the
507 shell and the standard output of each is run into the standard input of the
508 next.
509 The leftmost command in a pipeline will normally take its standard
510 input from the terminal and the rightmost will place its standard
511 output on the terminal.
512 Other examples of pipelines will be given later when we discuss the
513 history mechanism;
514 one important use of pipes which is illustrated there is in the
515 routing of information to the line printer.
516 .NH 2
517 Filenames
518 .PP
519 Many commands to be executed will need the names of files as arguments.
520 \s-2UNIX\s0
521 .I pathnames
522 consist of a number of
523 .I components
524 separated by `/'.
525 Each component except the last names a directory in which the next
526 component resides, in effect specifying the
527 .I path
528 of directories to follow to reach the file.
529 Thus the pathname
530 .DS
531 /etc/motd
532 .DE
533 specifies a file in the directory
534 `etc'
535 which is a subdirectory of the
536 .I root
537 directory `/'.
538 Within this directory the file named is `motd' which stands
539 for `message of the day'.
540 A
541 .I pathname
542 that begins with a slash is said to be an
543 .I absolute
544 pathname since it is specified from the absolute top of the entire
545 directory hierarchy of the system (the
546 .I root ).
547 .I Pathnames
548 which do not begin with `/' are interpreted as starting in the current
549 .I "working directory" ,
550 which is, by default, your
551 .I home
552 directory and can be changed dynamically by the
553 .I cd
554 change directory command.
555 Such pathnames are said to be
556 .I relative
557 to the working directory since they are found by starting
558 in the working directory and descending to lower levels of directories
559 for each
560 .I component
561 of the pathname.  If the pathname contains no slashes at all then the
562 file is contained in the working directory itself and the pathname is merely
563 the name of the file in this directory.
564 Absolute pathnames have no relation
565 to the working directory.
566 .PP
567 Most filenames consist of a number of alphanumeric characters and
568 `.'s (periods).
569 In fact, all printing characters except `/' (slash) may appear in filenames.
570 It is inconvenient to have most non-alphabetic characters in filenames
571 because many of these have special meaning to the shell.
572 The character `.' (period) is not a shell-metacharacter and is often used
573 to separate the
574 .I extension
575 of a file name from the base of the name.
576 Thus
577 .DS
578 prog.c prog.o prog.errs prog.output
579 .DE
580 are four related files.
581 They share a
582 .I base
583 portion of a name
584 (a base portion being that part of the name that is left when a trailing
585 `.' and following characters which are not `.' are stripped off).
586 The file
587 `prog.c'
588 might be the source for a C program,
589 the file `prog.o' the corresponding object file,
590 the file
591 `prog.errs' the errors resulting from a compilation of the program
592 and the file
593 `prog.output' the output of a run of the program.
594 .PP
595 If we wished to refer to all four of these files in a command, we could
596 use the notation
597 .DS
598 prog.*
599 .DE
600 This expression is expanded by the shell, before the command to which it is
601 an argument is executed, into a list of names which begin with `prog.'.
602 The character `*' here matches any sequence (including the empty sequence)
603 of characters in a file name.
604 The names which match are alphabetically sorted and placed in the
605 .I "argument list"
606 of the command.
607 Thus the command
608 .DS
609 echo prog.*
610 .DE
611 will echo the names
612 .DS
613 prog.c prog.errs prog.o prog.output
614 .DE
615 Note that the names are in sorted order here, and a different
616 order than we listed them above.
617 The
618 .I echo
619 command receives four words as arguments, even though we only typed
620 one word as an argument directly.
621 The four words were generated by
622 .I "filename expansion"
623 of the one input word.
624 .PP
625 Other notations for
626 .I "filename expansion"
627 are also available.
628 The character `?' matches any single character in a filename.
629 Thus
630 .DS
631 echo ? \|?? \|???
632 .DE
633 will echo a line of filenames; first those with one character names,
634 then those with two character names, and finally those with three
635 character names.
636 The names of each length will be independently sorted.
637 .PP
638 Another mechanism consists of a sequence of characters between `[' and `]'.
639 This metasequence matches any single character from the enclosed set.
640 Thus
641 .DS
642 prog.[co]
643 .DE
644 will match
645 .DS
646 prog.c prog.o
647 .DE
648 in the example above.
649 We can also place two characters around a `\-' in this notation to denote
650 a range.
651 Thus
652 .DS
653 chap.[1\-5]
654 .DE
655 might match files
656 .DS
657 chap.1 chap.2 chap.3 chap.4 chap.5
658 .DE
659 if they existed.
660 This is shorthand for
661 .DS
662 chap.[12345]
663 .DE
664 and otherwise equivalent.
665 .PP
666 An important point to note is that if a list of argument words to
667 a command (an
668 .I "argument list)"
669 contains filename expansion syntax, and if this filename expansion syntax
670 fails to match any existing file names, then the shell considers this
671 to be an error and prints a diagnostic
672 .DS
673 No match.
674 .DE
675 and does not execute the command.
676 .PP
677 Another very important point is that files with the character `.' at the
678 beginning are treated specially.
679 Neither `*' or `?' or the `[' `]' mechanism will match it.
680 This prevents accidental matching of the filenames `.' and `..'
681 in the working directory which have special meaning to the system,
682 as well as other files such as
683 .I \&.cshrc
684 which are not normally
685 visible.
686 We will discuss the special role of the file
687 .I \&.cshrc
688 later.
689 .PP
690 Another filename expansion mechanism gives access to the pathname of
691 the
692 .I home
693 directory of other users.
694 This notation consists of the character `~' (tilde) followed by another user's
695 login name.
696 For instance the word `~bill' would map to the pathname `/usr/bill'
697 if the home directory for `bill' was `/usr/bill'.
698 Since, on large systems, users may have login directories scattered over
699 many different disk volumes with different prefix directory names,
700 this notation provides a convenient way of accessing the files
701 of other users.
702 .PP
703 A special case of this notation consists of a `~' alone, e.g. `~/mbox'.
704 This notation is expanded by the shell into the file `mbox' in your
705 .I home
706 directory, i.e. into `/usr/bill/mbox' for me on Ernie Co-vax, the UCB
707 Computer Science Department VAX machine, where this document was prepared.
708 This can be very useful if you have used
709 .I cd
710 to change to another directory and have found a file you wish to
711 copy using
712 .I cp.
713 If I give the command
714 .DS
715 cp thatfile ~
716 .DE
717 the shell will expand this command to
718 .DS
719 cp thatfile /usr/bill
720 .DE
721 since my home directory is /usr/bill.
722 .PP
723 There also exists a mechanism using the characters `{' and `}' for
724 abbreviating a set of words which have common parts but cannot
725 be abbreviated by the above mechanisms because they are not files,
726 are the names of files which do not yet exist,
727 are not thus conveniently described.
728 This mechanism will be described much later,
729 in section 4.2,
730 as it is used less frequently.
731 .NH 2
732 Quotation
733 .PP
734 We have already seen a number of metacharacters used by the shell.
735 These metacharacters pose a problem in that we cannot use them directly
736 as parts of words.
737 Thus the command
738 .DS
739 echo *
740 .DE
741 will not echo the character `*'.
742 It will either echo a sorted list of filenames in the
743 current
744 .I "working directory,"
745 or print the message `No match' if there are
746 no files in the working directory.
747 .PP
748 The recommended mechanism for placing characters which are neither numbers,
749 digits, `/', `.' or `\-' in an argument word to a command is to enclose
750 it with single quotation characters `\'', i.e.
751 .DS
752 echo \'*\'
753 .DE
754 There is one special character `!' which is used by the
755 .I history
756 mechanism of the shell and which cannot be
757 .I escaped
758 by placing it within `\'' characters.
759 It and the character `\'' itself can be preceded by a single `\e'
760 to prevent their special meaning.
761 Thus
762 .DS
763 echo \e\'\e!
764 .DE
765 prints
766 .DS
767 \'!
768 .DE
769 These two mechanisms suffice to place any printing character into a word
770 which is an argument to a shell command.  They can be combined, as in
771 .DS
772 echo \e\'\'*\'
773 .DE
774 which prints
775 .DS
776 \'*
777 .DE
778 since the first `\e' escaped the first `\'' and the `*' was enclosed
779 between `\'' characters.
780 .NH 2
781 Terminating commands
782 .PP
783 When you are executing a command and the shell is
784 waiting for it to complete there are several ways
785 to force it to stop.
786 For instance if you type the command
787 .DS
788 cat /etc/passwd
789 .DE
790 the system will print a copy of a list of all users of the system
791 on your terminal.
792 This is likely to continue for several minutes unless you stop it.
793 You can send an
794 \s-2INTERRUPT\s0
795 .I signal
796 to the
797 .I cat
798 command by typing ^C on your terminal.*
799 .FS
800 *On some older Unix systems the \s-2DEL\s0 or \s-2RUBOUT\s0 key
801 has the same effect. "stty all" will tell you the INTR key value.
802 .FE
803 Since
804 .I cat
805 does not take any precautions to avoid or otherwise handle this signal
806 the
807 \s-2INTERRUPT\s0
808 will cause it to terminate.
809 The shell notices that
810 .I cat
811 has terminated and prompts you again with `% '.
812 If you hit \s-2INTERRUPT\s0 again, the shell will just
813 repeat its prompt since it handles \s-2INTERRUPT\s0 signals
814 and chooses to continue to execute commands rather than terminating
815 like
816 .I cat
817 did, which would have the effect of logging you out.
818 .PP
819 Another way in which many programs terminate is when they get an end-of-file
820 from their standard input.
821 Thus the
822 .I mail
823 program in the first example above was terminated when we typed a ^D
824 which generates an end-of-file from the standard input.
825 The shell also terminates when it gets an end-of-file printing `logout';
826 \s-2UNIX\s0 then logs you off the system.
827 Since this means that typing too many ^D's can accidentally log us off,
828 the shell has a mechanism for preventing this.
829 This
830 .I ignoreeof
831 option will be discussed in section 2.2.
832 .PP
833 If a command has its standard input redirected from a file, then it will
834 normally terminate when it reaches the end of this file.
835 Thus if we execute
836 .DS
837 mail bill < prepared.text
838 .DE
839 the mail command will terminate without our typing a ^D.
840 This is because it read to the end-of-file of our file
841 `prepared.text' in which we placed a message for `bill' with an editor program.
842 We could also have done
843 .DS
844 cat prepared.text \||\| mail bill
845 .DE
846 since the
847 .I cat
848 command would then have written the text through the pipe to the
849 standard input of the mail command.
850 When the
851 .I cat
852 command completed it would have terminated,
853 closing down the pipeline
854 and the
855 .I mail
856 command would have received an end-of-file from it and terminated.
857 Using a pipe here is more complicated than redirecting input
858 so we would more likely use the first form.
859 These commands could also have been stopped by sending an \s-2INTERRUPT\s0.
860 .PP
861 Another possibility for stopping a command is to suspend its execution
862 temporarily, with the possibility of continuing execution later.  This is
863 done by sending a \s-2STOP\s0 signal via typing a ^Z.
864 This signal causes all commands running on the terminal
865 (usually one but more if a pipeline is executing) to become suspended.
866 The shell notices that the command(s) have been suspended, types
867 `Stopped' and then prompts for a new command.
868 The previously executing command has been suspended, but otherwise
869 unaffected by the \s-2STOP\s0 signal.  Any other commands can be executed
870 while the original command remains suspended.  The suspended command can
871 be continued using the
872 .I fg
873 command with no arguments.  The shell will then retype the command
874 to remind you which command is being continued, and cause the command
875 to resume execution.  Unless any input files in use by the suspended
876 command have been changed in the meantime, the suspension has no effect
877 whatsoever on the execution of the command.  This feature can be very useful
878 during editing, when you need to look at another file before continuing.
879 An
880 example of command suspension follows.
881 .DS
882 % mail harold
883 Someone just copied a big file into my directory and its name is
884 ^Z
885 Stopped
886 % ls
887 funnyfile
888 prog.c
889 prog.o
890 % jobs
891 .ta 1.75i
892 [1]  + Stopped  mail harold
893 % fg
894 mail harold
895 funnyfile. Do you know who did it?
896 EOT
897 %
898 .so tabs
899 .DE
900 In this example someone was sending a message to Harold and forgot the
901 name of the file he wanted to mention.  The mail command was suspended
902 by typing ^Z.  When the shell noticed that the mail program was
903 suspended, it typed `Stopped' and prompted for a new command.  Then the
904 .I ls
905 command was typed to find out the name of the file.  The
906 .I jobs
907 command was run to find out which command was suspended.
908 At this time the
909 .I fg
910 command was typed to continue execution of the mail program.  Input
911 to the mail program was then continued and ended with a ^D
912 which indicated the end of the message at which time the mail
913 program typed EOT.  The
914 .I jobs
915 command will show which commands are suspended.
916 The ^Z should only be typed at the beginning of a line since
917 everything typed on the current line is discarded when a signal is sent
918 from the keyboard.  This also happens on \s-2INTERRUPT\s0, and \s-2QUIT\s0
919 signals.  More information on
920 suspending jobs and controlling them is given in
921 section 2.6.
922 .PP
923 If you write or run programs which are not fully debugged then it may
924 be necessary to stop them somewhat ungracefully.
925 This can be done by sending them a \s-2QUIT\s0
926 signal, sent by typing a ^\e.
927 This will usually provoke the shell to produce a message like:
928 .DS
929 Quit (Core dumped)
930 .DE
931 indicating that a file
932 `core' has been created containing information about the running program's
933 state when it terminated due to the \s-2QUIT\s0 signal.
934 You can examine this file yourself, or forward information to the
935 maintainer of the program telling him/her where the
936 .I "core file"
937 is.
938 .PP
939 If you run background commands (as explained in section 2.6) then these
940 commands will ignore \s-2INTERRUPT\s0 and \s-2QUIT\s0 signals at the
941 terminal.  To stop them you must use the
942 .I kill
943 command.  See section 2.6 for an example.
944 .PP
945 If you want to examine the output of a command without having it move
946 off the screen as the output of the
947 .DS
948 cat /etc/passwd
949 .DE
950 command will, you can use the command
951 .DS
952 more /etc/passwd
953 .DE
954 The
955 .I more
956 program pauses after each complete screenful and types `\-\-More\-\-'
957 at which point you can hit a space to get another screenful, a return
958 to get another line, a `?' to get some help on other commands, or a `q' to end the
959 .I more
960 program.  You can also use more as a filter, i.e.
961 .DS
962 cat /etc/passwd | more
963 .DE
964 works just like the more simple more command above.
965 .PP
966 For stopping output of commands not involving
967 .I more
968 you can use the
969 ^S key to stop the typeout.  The typeout will resume when you
970 hit ^Q or any other key, but ^Q is normally used because
971 it only restarts the output and does not become input to the program
972 which is running.  This works well on low-speed terminals, but at 9600
973 baud it is hard to type ^S and ^Q fast enough to paginate
974 the output nicely, and a program like
975 .I more
976 is usually used.
977 .PP
978 An additional possibility is to use the ^O flush output
979 character; when this character is typed, all output from the current
980 command is thrown away (quickly) until the next input read occurs
981 or until the next shell prompt.  This can be used to allow a command
982 to complete without having to suffer through the output on a slow
983 terminal; ^O is a toggle, so flushing can be turned off by
984 typing ^O again while output is being flushed.
985 .NH 2
986 What now?
987 .PP
988 We have so far seen a number of mechanisms of the shell and learned a lot
989 about the way in which it operates.
990 The remaining sections will go yet further into the internals of the
991 shell, but you will surely want to try using the
992 shell before you go any further.
993 To try it you can log in to \s-2UNIX\s0 and type the following
994 command to the system:
995 .DS
996 chsh myname /bin/csh
997 .DE
998 Here `myname' should be replaced by the name you typed to
999 the system prompt of `login:' to get onto the system.
1000 Thus I would use `chsh bill /bin/csh'.
1001 .B
1002 You only have to do this once; it takes effect at next login.
1003 .R
1004 You are now ready to try using
1005 .I csh.
1006 .PP
1007 Before you do the `chsh' command, the shell you are using when
1008 you log into the system is `/bin/sh'.
1009 In fact, much of the above discussion is applicable to `/bin/sh'.
1010 The next section will introduce many features particular to
1011 .I csh
1012 so you should change your shell to
1013 .I csh
1014 before you begin reading it.
1015 .bp