Commit | Line | Data |
---|---|---|
5ef57bfd BJ |
1 | .RP |
2 | .TL | |
3 | An introduction to the C shell | |
4 | .br | |
5 | \s-2\fI(Revised for the Third Berkeley Distribution)\fP\s0 | |
6 | .AU | |
7 | William Joy | |
8 | .AI | |
9 | Computer Science Division | |
10 | .br | |
11 | Department of Electrical Engineering and Computer Science | |
12 | .br | |
13 | University of California, Berkeley | |
14 | .br | |
15 | Berkeley, California 94720 | |
16 | .AB | |
17 | .I Csh | |
18 | is a new command language interpreter for | |
19 | .UX | |
20 | systems. | |
21 | It incorporates good features of other shells and a | |
22 | .I history | |
23 | mechanism similar to the | |
24 | .I redo | |
25 | of \s-2INTERLISP\s0. | |
26 | While incorporating many features of other shells which make | |
27 | writing shell programs (shell scripts) easier, | |
28 | most of the features unique to | |
29 | .I csh | |
30 | are designed more for the interactive \s-2UNIX\s0 user. | |
31 | .PP | |
32 | \s-2UNIX\s0 | |
33 | users who have read a general introduction to the system | |
34 | will find a valuable basic explanation of the shell here. | |
35 | Simple terminal interaction with | |
36 | .I csh | |
37 | is possible after reading just the first section of this document. | |
38 | The second section describes the shells capabilities which you can | |
39 | explore after you have begun to become acquainted with the shell. | |
40 | Later sections introduce features which are useful, but not necessary | |
41 | for all users of the shell. | |
42 | .PP | |
43 | Back matter includes an appendix listing special characters of the shell | |
44 | and a glossary of terms and commands introduced in this manual. | |
45 | .AE | |
46 | .SH | |
47 | .if n .ND | |
48 | Introduction | |
49 | .PP | |
50 | A | |
51 | .I shell | |
52 | is a command language interpreter. | |
53 | .I Csh | |
54 | is the name of one particular command interpreter on | |
55 | \s-2UNIX\s0. | |
56 | The primary purpose of | |
57 | .I csh | |
58 | is to translate command lines typed at a terminal into | |
59 | system actions, such as invocation of other programs. | |
60 | .I Csh | |
61 | is a user program just like any you might write. | |
62 | Hopefully, | |
63 | .I csh | |
64 | will be a very useful program for you | |
65 | in interacting with the \s-2UNIX\s0 system. | |
66 | .PP | |
67 | In addition to this document, you will want to refer to a copy | |
68 | of the ``\s-2UNIX\s0 Programmers Manual.'' | |
69 | The | |
70 | .I csh | |
71 | documentation in the manual provides a full description of all | |
72 | features of the shell and is a final reference for questions | |
73 | about the shell. | |
74 | .PP | |
75 | Many words in this document are shown in | |
76 | .I italics. | |
77 | These are important words; | |
78 | names of commands, and words which have special meaning in discussing | |
79 | the shell and \s-2UNIX\s0. | |
80 | Many of the words are defined in a glossary at the end of this document. | |
81 | If you don't know what is meant by a word, you should look | |
82 | for it in the glossary. | |
83 | .SH | |
84 | Acknowledgements | |
85 | .PP | |
86 | Numerous people have provided good input about previous versions | |
87 | of | |
88 | .I csh | |
89 | and aided in its debugging and in the debugging of its documentation. | |
90 | I would especially like to thank Michael Ubell | |
91 | who made the crucial observation that history commands could be | |
92 | done well over the word structure of input text, and implemented | |
93 | a prototype history mechanism in an older version of the shell. | |
94 | Eric Allman has also provided a large number of useful comments on the | |
95 | shell, helping to unify those concepts which are present and to identify | |
96 | and eliminate useless and marginally useful features. | |
97 | Mike O'Brien suggested the pathname hashing | |
98 | mechanism which speeds command execution. | |
99 | .br | |
100 | .bp | |
101 | .NH | |
102 | Terminal usage of the shell | |
103 | .NH 2 | |
104 | The basic notion of commands | |
105 | .PP | |
106 | A | |
107 | .I shell | |
108 | in | |
109 | \s-2UNIX\s0 | |
110 | acts mostly as a medium through which other | |
111 | .I commands | |
112 | are invoked. | |
113 | While it has a set of | |
114 | .I builtin | |
115 | commands which it performs directly, | |
116 | most useful commands are, in fact, external to the shell. | |
117 | The shell is thus distinguished from the command interpreters of other | |
118 | systems both by the fact that it is just a user program, and by the fact | |
119 | that it is used almost exclusively as a mechanism for invoking other programs. | |
120 | .PP | |
121 | Commands in the \s-2UNIX\s0 system expect a list of strings or | |
122 | .I words | |
123 | as arguments. | |
124 | Thus the command | |
125 | .DS | |
126 | mail bill | |
127 | .DE | |
128 | consists of two words. | |
129 | The first word | |
130 | .I mail | |
131 | names the command to be executed, in this case the | |
132 | mail program which sends messages to other users. | |
133 | The shell uses the name of the command in attempting to run it for you. | |
134 | It will look in a number of | |
135 | .I directories | |
136 | for a file with the name | |
137 | .I mail | |
138 | which is expected to contain the mail program. | |
139 | .PP | |
140 | The rest of the words of the command are given to the command itself to execute. | |
141 | In this case we specified also the word | |
142 | .I bill | |
143 | which is interpreted by the | |
144 | .I mail | |
145 | program to be the name of a user to whom mail is to be sent. | |
146 | In normal terminal usage we might use the | |
147 | .I mail | |
148 | command as follows. | |
149 | .DS | |
150 | % mail bill | |
151 | I have a question about the csh documentation. | |
152 | My document seems to be missing page 5. | |
153 | Does a page five exist? | |
154 | Bill | |
155 | EOT | |
156 | % | |
157 | .DE | |
158 | .PP | |
159 | Here we typed a message to send to | |
160 | .I bill | |
161 | and ended this message with a control-d which sent an end-of-file to | |
162 | the mail program. | |
163 | The mail program | |
164 | then echoed the characters `EOT' and transmitted our message. | |
165 | The characters `% ' were printed before and after the mail command | |
166 | by the shell to indicate that input was needed. | |
167 | .PP | |
168 | After typing the `% ' prompt the shell was reading command input from | |
169 | our terminal. | |
170 | We typed a complete command `mail bill'. | |
171 | The shell then executed the | |
172 | .I mail | |
173 | program with argument | |
174 | .I bill | |
175 | and went dormant waiting for it to complete. | |
176 | The mail program then read input from our terminal until we signalled | |
177 | an end-of-file after which the shell noticed that mail had completed | |
178 | and signaled us that it was ready to read from the terminal again by | |
179 | printing another `% ' prompt. | |
180 | .PP | |
181 | This is the essential pattern of all interaction with \s-2UNIX\s0 | |
182 | through the shell. | |
183 | A complete command is typed at the terminal, the shell executes | |
184 | the command and when this execution completes prompts for a new command. | |
185 | If you run the editor for an hour, the shell will patiently wait for | |
186 | you to finish editing and obediently prompt you again whenever you finish | |
187 | editing. | |
188 | .PP | |
189 | An example of a useful command you can execute now is the | |
190 | .I tset | |
191 | command, which sets the default | |
192 | .I erase | |
193 | and | |
194 | .I kill | |
195 | characters on your terminal \- the erase character erases the last | |
196 | character you typed and the kill character erases the entire line you | |
197 | have entered so far. | |
198 | By default, the erase character is `#' and the kill character is `@'. | |
199 | Most people who use \s-2CRT\s0 displays prefer to use the backspace | |
200 | (control-h) character as their erase character since it is then easier | |
201 | to see what you have typed so far. | |
202 | You can make this be true by typing | |
203 | .DS | |
204 | tset \-e | |
205 | .DE | |
206 | which tells the program | |
207 | .I tset | |
208 | to set the erase character, and its default setting for this character | |
209 | is a backspace. | |
210 | .NH 2 | |
211 | Flag arguments | |
212 | .PP | |
213 | A useful notion in \s-2UNIX\s0 is that of a | |
214 | .I flag | |
215 | argument. | |
216 | While many arguments to commands specify file names or user names | |
217 | some arguments rather specify an optional capability of the command | |
218 | which you wish to invoke. | |
219 | By convention, such arguments begin with the character `\-'. | |
220 | Thus the command | |
221 | .DS | |
222 | ls | |
223 | .DE | |
224 | will produce a list of the files in the current directory. | |
225 | The option | |
226 | .I \-s | |
227 | is the size option, and | |
228 | .DS | |
229 | ls \-s | |
230 | .DE | |
231 | causes | |
232 | .I ls | |
233 | to also give, for each file the size of the file in blocks of 512 | |
234 | characters. | |
235 | The manual page for each command in the \s-2UNIX\s0 programmers manual | |
236 | gives the available options for each command. | |
237 | The | |
238 | .I ls | |
239 | command has a large number of useful and interesting options. | |
240 | Most other commands have either no options or only one or two options. | |
241 | It is hard to remember options of commands which are not used very | |
242 | frequently, so most \s-2UNIX\s0 utilities perform only one or two functions | |
243 | rather than having a large number of hard to remember options. | |
244 | .NH 2 | |
245 | Output to files | |
246 | .PP | |
247 | Many commands may read input or write output to files rather | |
248 | than simply taking input and output from the terminal. | |
249 | Each such command could take special words as arguments indicating | |
250 | where the output is to go. | |
251 | It is simpler, and usually | |
252 | sufficient, to connect these commands to files to which they wish to | |
253 | write, within the shell itself, and just before they are executed. | |
254 | .PP | |
255 | Thus suppose we wish to save the current date in a file called `now'. | |
256 | The command | |
257 | .DS | |
258 | date | |
259 | .DE | |
260 | will print the current date on our terminal. | |
261 | This is because our terminal is the default | |
262 | .I "standard output" | |
263 | for the date command and the date command prints the date on its | |
264 | standard output. | |
265 | The shell lets us redirect the | |
266 | .I "standard output" | |
267 | of a command through a | |
268 | notation using the | |
269 | .I metacharacter | |
270 | `>' and the name of the file where output is to be placed. | |
271 | Thus the command | |
272 | .DS | |
273 | date > now | |
274 | .DE | |
275 | runs the | |
276 | .I date | |
277 | command in an environment where its standard output is | |
278 | the file `now' rather than our terminal. | |
279 | Thus this command places the current date and time in the file `now'. | |
280 | It is important to know that the | |
281 | .I date | |
282 | command was unaware that its output was going to a file rather than | |
283 | to our terminal. | |
284 | The shell performed this | |
285 | .I redirection | |
286 | before the command began executing. | |
287 | .PP | |
288 | One other thing to note here is that the file `now' | |
289 | need not have existed before the | |
290 | .I date | |
291 | command was executed; the shell would have created the file if it did | |
292 | not exist. | |
293 | And if the file did exist? | |
294 | If it had existed previously these previous contents would have been discarded! | |
295 | A shell option | |
296 | .I noclobber | |
297 | exists to prevent this from happening accidentally; | |
298 | it is discussed in section 2.2. | |
299 | .PP | |
300 | The system normally keeps files which you create with `>' and all other files. | |
301 | Thus the default is for files to be permanent. If you wish to create a file | |
302 | which will be removed automatically, you can begin its name with a `#' | |
303 | character, this `scratch' character denotes the fact that the file will | |
304 | be a scratch file.* | |
305 | .FS | |
306 | *Note that if your erase character is a `#', you will have to precede the | |
307 | `#' with a `\e'. The fact that the `#' character is the old (pre-\s-2CRT\s0) | |
308 | standard erase character means that it seldom appears in a file name, and | |
309 | allows this convention to be used for scratch files. If you are using a | |
310 | \s-2CRT\s0, your erase character should be a control-h, as we demonstrated | |
311 | in section 1.1 how this could be set up. | |
312 | .FE | |
313 | The system will remove such files after a couple of days, | |
314 | or sooner if file space becomes very tight. | |
315 | Thus, in running the | |
316 | .I date | |
317 | command above, we don't really want to save the output forever, so we | |
318 | would more likely do | |
319 | .DS | |
320 | date > #now | |
321 | .DE | |
322 | .NH 2 | |
323 | Metacharacters in the shell | |
324 | .PP | |
325 | The shell has a large number of | |
326 | special characters (like `>') | |
327 | which indicate special functions. | |
328 | We say that these notations have | |
329 | .I syntactic | |
330 | and | |
331 | .I semantic | |
332 | meaning to the shell. | |
333 | In general, most characters which are neither letters nor digits | |
334 | have special meaning to the shell. | |
335 | We shall shortly learn a means of | |
336 | .I quotation | |
337 | which allows us to create words which contain | |
338 | .I metacharacters | |
339 | and to thus work without constantly worrying about whether certain | |
340 | characters are metacharacters. | |
341 | .PP | |
342 | Note that the shell is only reading input when it has prompted with | |
343 | `% '. | |
344 | Thus metacharacters will normally have effect only then. | |
345 | We need not worry about placing shell metacharacters in a letter | |
346 | we are sending via | |
347 | .I mail. | |
348 | .NH 2 | |
349 | Input from files; pipelines | |
350 | .PP | |
351 | We learned above how to route the standard output of a command | |
352 | to a file. | |
353 | It is also possible to route the standard input of a command from a file. | |
354 | This is not often necessary since most commands will read from | |
355 | a file name given as argument. | |
356 | We can give the command | |
357 | .DS | |
358 | sort < data | |
359 | .DE | |
360 | to run the | |
361 | .I sort | |
362 | command with standard input, where the command normally | |
363 | reads, from the file | |
364 | `data'. | |
365 | We would more likely say | |
366 | .DS | |
367 | sort data | |
368 | .DE | |
369 | letting the | |
370 | .I sort | |
371 | command open the file | |
372 | `data' | |
373 | for input itself since this is less to type. | |
374 | .PP | |
375 | We should note that if we just typed | |
376 | .DS | |
377 | sort | |
378 | .DE | |
379 | then the sort program would sort lines from its | |
380 | .I "standard input." | |
381 | Since we did not | |
382 | .I redirect | |
383 | the standard input, it would sort lines as we typed them on the terminal | |
384 | until we typed a control-d to generate an end-of-file. | |
385 | .PP | |
386 | A most useful capability is the ability to combine the standard output | |
387 | of one command with the standard input of the next, i.e. to run the | |
388 | commands in a sequence known as a | |
389 | .I pipeline. | |
390 | For instance the command | |
391 | .DS | |
392 | ls \-s | |
393 | .DE | |
394 | normally produces a list of the files in our directory with the size | |
395 | of each in blocks of 512 characters. | |
396 | If we are interested in learning which of our files is largest we | |
397 | may wish to have this sorted by size rather than by name, which is | |
398 | the default way in which | |
399 | .I ls | |
400 | sorts. | |
401 | We could look at the many options of | |
402 | .I ls | |
403 | to see if there was an option to do this but would eventually discover | |
404 | that there is not. | |
405 | Instead we can use a couple of simple options of the | |
406 | .I sort | |
407 | command, combining it with | |
408 | .I ls | |
409 | to get what we want. | |
410 | .PP | |
411 | The | |
412 | .I \-n | |
413 | option of sort specifies a numeric sort rather than an alphabetic sort. | |
414 | Thus | |
415 | .DS | |
416 | ls \-s | sort \-n | |
417 | .DE | |
418 | specifies that the output of the | |
419 | .I ls | |
420 | command run with the option | |
421 | .I \-s | |
422 | is to be | |
423 | .I piped | |
424 | to the command | |
425 | .I sort | |
426 | run with the numeric sort option. | |
427 | This would give us a sorted list of our files by size, but with the | |
428 | smallest first. | |
429 | We could then use the | |
430 | .I \-r | |
431 | reverse sort option and the | |
432 | .I head | |
433 | command in combination with the previous command doing | |
434 | .DS | |
435 | ls \-s | sort \-n \-r | head \-5 | |
436 | .DE | |
437 | Here we have taken a list of our files sorted alphabetically, | |
438 | each with the size in blocks. | |
439 | We have run this to the standard input of the | |
440 | .I sort | |
441 | command asking it to sort numerically in reverse order (largest first). | |
442 | This output has then been run into the command | |
443 | .I head | |
444 | which gives us the first few lines out. | |
445 | In this case we have asked | |
446 | .I head | |
447 | for the first 5 lines. | |
448 | Thus this command gives us the names and sizes of our 5 largest files. | |
449 | .PP | |
450 | The metanotation introduced above is called the | |
451 | .I pipe | |
452 | mechanism. | |
453 | Commands separated by `\||\|' characters are connected together by the | |
454 | shell and the output of each is run into the input of the next. | |
455 | The leftmost command in a pipeline will normally take its standard | |
456 | input from the terminal and the rightmost will place its standard | |
457 | output on the terminal. | |
458 | Other examples of pipelines will be given later when we discuss the | |
459 | history mechanism; | |
460 | one important use of pipes which is illustrated there is in the | |
461 | routing of information to the line printer. | |
462 | .NH 2 | |
463 | Filenames | |
464 | .PP | |
465 | Many commands to be executed will need the names of files as arguments. | |
466 | \s-2UNIX\s0 pathnames consist of a number of components separated by `/'. | |
467 | Each component except the last names a directory in which the next | |
468 | component resides. | |
469 | Thus the pathname | |
470 | .DS | |
471 | /etc/motd | |
472 | .DE | |
473 | specifies a file in the directory | |
474 | `etc' | |
475 | which is a subdirectory of the | |
476 | .I root | |
477 | directory `/'. | |
478 | Within this directory the file named is `motd' which stands | |
479 | for `message of the day'. | |
480 | Filenames which do not begin with `/' are interpreted starting at | |
481 | the current | |
482 | .I working | |
483 | directory. | |
484 | This directory is, by default, your | |
485 | .I home | |
486 | directory and can be changed dynamically by the | |
487 | .I chdir | |
488 | change directory command. | |
489 | .PP | |
490 | Most filenames consist of a number of alphanumeric characters and `.'s. | |
491 | In fact, all printing characters except `/' may appear in filenames. | |
492 | It is inconvenient to have most non-alphabetic characters in filenames | |
493 | because many of these have special meaning to the shell. | |
494 | The character `.' is not a shell-metacharacter and is often used as | |
495 | the prefix with an | |
496 | .I extension | |
497 | of a base name. | |
498 | Thus | |
499 | .DS | |
500 | prog.c prog.o prog.errs prog.output | |
501 | .DE | |
502 | are four related files. | |
503 | They share a | |
504 | .I root | |
505 | portion of a name | |
506 | (a root portion being that part of the name that is left when a trailing | |
507 | `.' and following characters which are not `.' are stripped off). | |
508 | The file | |
509 | `prog.c' | |
510 | might be the source for a C program, | |
511 | the file `prog.o' the corresponding object file, | |
512 | the file | |
513 | `prog.errs' the errors resulting from a compilation of the program | |
514 | and the file | |
515 | `prog.output' the output of a run of the program. | |
516 | .PP | |
517 | If we wished to refer to all four of these files in a command, we could | |
518 | use the metanotation | |
519 | .DS | |
520 | prog.* | |
521 | .DE | |
522 | This word is expanded by the shell, before the command to which it is | |
523 | an argument is executed, into a list of names which begin with `prog.'. | |
524 | The character `*' here matches any sequence (including the empty sequence) | |
525 | of characters in a file name. | |
526 | The names which match are sorted into the argument list to the command | |
527 | alphabetically. | |
528 | Thus the command | |
529 | .DS | |
530 | echo prog.* | |
531 | .DE | |
532 | will echo the names | |
533 | .DS | |
534 | prog.c prog.errs prog.o prog.output | |
535 | .DE | |
536 | Note that the names are in lexicographic order here, and a different | |
537 | order than we listed them above. | |
538 | The | |
539 | .I echo | |
540 | command receives four words as arguments, even though we only typed | |
541 | one word as as argument directly. | |
542 | The four words were generated by filename expansion of the metasyntax | |
543 | in the one input word. | |
544 | .PP | |
545 | Other metanotations for | |
546 | .I "filename expansion" | |
547 | are also available. | |
548 | The character `?' matches any single character in a filename. | |
549 | Thus | |
550 | .DS | |
551 | echo ? \|?? \|??? | |
552 | .DE | |
553 | will echo a line of filenames; first those with one character names, | |
554 | then those with two character names, and finally those with three | |
555 | character names. | |
556 | The names of each length will be independently lexicographically sorted. | |
557 | .PP | |
558 | Another mechanism consists of a sequence of characters between `[' and `]'. | |
559 | This metasequence matches any single character from the enclosed set. | |
560 | Thus | |
561 | .DS | |
562 | prog.[co] | |
563 | .DE | |
564 | will match | |
565 | .DS | |
566 | prog.c prog.o | |
567 | .DE | |
568 | in the example above. | |
569 | We can also place two characters astride a `\-' in this notation to denote | |
570 | a range. | |
571 | Thus | |
572 | .DS | |
573 | chap.[1\-5] | |
574 | .DE | |
575 | might match files | |
576 | .DS | |
577 | chap.1 chap.2 chap.3 chap.4 chap.5 | |
578 | .DE | |
579 | if they existed. | |
580 | This is shorthand for | |
581 | .DS | |
582 | chap.[12345] | |
583 | .DE | |
584 | and otherwise equivalent. | |
585 | .PP | |
586 | An important point to note is that if a list of argument words to | |
587 | a command (an | |
588 | .I "argument list)" | |
589 | contains filename expansion syntax, and if this filename expansion syntax | |
590 | fails to match any existing file names, then the shell considers this | |
591 | to be an error and prints a diagnostic | |
592 | .DS | |
593 | No match. | |
594 | .DE | |
595 | .PP | |
596 | Another very important point is that the character `.' at the beginning | |
597 | of a filename is treated specially. | |
598 | Neither `*' or `?' or the `[' `]' mechanism will match it. | |
599 | This prevents accidental matching of the filenames `.' and `..' | |
600 | in the current directory which have special meaning to the system, | |
601 | as well as other files such as | |
602 | .I \&.cshrc | |
603 | which are not normally | |
604 | visible. | |
605 | We will discuss the special role of the file | |
606 | .I \&.cshrc | |
607 | later. | |
608 | .PP | |
609 | Another filename expansion mechanism gives access to the pathname of | |
610 | the | |
611 | .I home | |
612 | directory of other users. | |
613 | This notation consists of the character `~' followed by another users | |
614 | login name. | |
615 | For instance the word `~bill' would map to the pathname `/usr/bill' | |
616 | if the home directory for `bill' was in the directory `/usr/bill'. | |
617 | Since, on large systems, users may have login directories scattered over | |
618 | many different disk volumes with different prefix directory names, | |
619 | this notation provides a reliable way of accessing the files | |
620 | of other users. | |
621 | .PP | |
622 | A special case of this notation consists of a `~' alone, e.g. `~/mbox'. | |
623 | This notation is expanded by the shell into the file `mbox' in your | |
624 | .I home | |
625 | directory, i.e. into `/usr/bill/mbox' for me on Ernie Co-vax, the UCB | |
626 | Computer Science Department Vax machine, where this document was prepared. | |
627 | This can be very useful if you have used | |
628 | .I chdir | |
629 | to change to another users directory and have found a file you wish to | |
630 | copy using | |
631 | .I cp. | |
632 | You can do | |
633 | .DS | |
634 | cp thatfile ~ | |
635 | .DE | |
636 | which will be expanded by the shell to | |
637 | .DS | |
638 | cp thatfile /usr/bill | |
639 | .DE | |
640 | e.g., which the copy command will interpret as a request to make a copy of | |
641 | `thatfile' | |
642 | in the directory `/usr/bill'. | |
643 | The `~' notation doesn't, by itself, force named files to exist. | |
644 | This is useful, for example, when using the | |
645 | .I cp | |
646 | command, | |
647 | e.g. | |
648 | .DS | |
649 | cp thatfile ~/saveit | |
650 | .DE | |
651 | .PP | |
652 | There also exists a mechanism using the characters `{' and `}' for | |
653 | abbreviating a set of word which have common parts but cannot | |
654 | be abbreviated by the above mechanisms because they are not files, | |
655 | are the names of files which do not yet exist, | |
656 | are not thus conveniently described. | |
657 | This mechanism will be described much later, | |
658 | in section 4.1, | |
659 | as it is used less frequently. | |
660 | .NH 2 | |
661 | Quotation | |
662 | .PP | |
663 | We have already seen a number of metacharacters used by the shell. | |
664 | These metacharacters pose a problem in that we cannot use them directly | |
665 | as parts of words. | |
666 | Thus the command | |
667 | .DS | |
668 | echo * | |
669 | .DE | |
670 | will not echo the character `*'. | |
671 | It will either echo an sorted list of filenames in the | |
672 | current directory, or print the message `No match' if there are | |
673 | no files in the current directory. | |
674 | .PP | |
675 | The recommended mechanism for placing characters which are neither numbers, | |
676 | digits, `/', `.' or `\-' in an argument word to a command is to enclose | |
677 | it with single quotation characters `\'', i.e. | |
678 | .DS | |
679 | echo \'*\' | |
680 | .DE | |
681 | There is one special character `!' which is used by the | |
682 | .I history | |
683 | mechanism of the shell and which cannot be | |
684 | .I escaped | |
685 | by placing it within \` characters. | |
686 | It and the character `\'' itself can be preceded by a single `\e' | |
687 | to prevent their special meaning. | |
688 | Thus | |
689 | .DS | |
690 | echo \e\'\e! | |
691 | .DE | |
692 | prints | |
693 | .DS | |
694 | \'! | |
695 | .DE | |
696 | These two mechanisms suffice to place any printing character into a word | |
697 | which is an argument to a shell command. They can be combined, as in | |
698 | .DS | |
699 | echo \e\'\'*\' | |
700 | .DE | |
701 | which prints | |
702 | .DS | |
703 | \'* | |
704 | .DE | |
705 | .NH 2 | |
706 | Terminating commands | |
707 | .PP | |
708 | When you are running a command from the shell and the shell is | |
709 | dormant waiting for it to complete there are a couple of ways in | |
710 | which you can force such a command to stop. | |
711 | For instance if you type the command | |
712 | .DS | |
713 | cat /etc/passwd | |
714 | .DE | |
715 | the system will print a copy of a list of all users of the system | |
716 | on your terminal. | |
717 | This is likely to continue for several minutes unless you stop it. | |
718 | You can send an | |
719 | \s-2INTERRUPT\s0 | |
720 | signal to the | |
721 | .I cat | |
722 | command by hitting the | |
723 | \s-2DEL\s0 or \s-2RUBOUT\s0 | |
724 | key on your terminal. | |
725 | Actually, hitting this key sends this | |
726 | \s-2INTERRUPT\s0 | |
727 | signal to all programs running on your terminal, including your shell. | |
728 | The shell normally ignores such signals however, so that the only | |
729 | program affected by the | |
730 | \s-2INTERRUPT\s0 | |
731 | will be | |
732 | .I cat. | |
733 | Since | |
734 | .I cat | |
735 | does not take any precautions to catch this signal | |
736 | the | |
737 | \s-2INTERRUPT\s0 | |
738 | will cause it to terminate. | |
739 | The shell notices that | |
740 | .I cat | |
741 | has died and prompts you again with `% '. | |
742 | If you hit \s-2INTERRUPT\s0 again, the shell will just | |
743 | repeat its prompt since it catches \s-2INTERRUPT\s0 signals | |
744 | and chooses to continue to execute commands rather than going away | |
745 | like | |
746 | .I cat | |
747 | did, which would have the effect of logging you out. | |
748 | .PP | |
749 | Another way in which many programs terminate is when they get an end-of-file | |
750 | from their standard input. | |
751 | Thus the | |
752 | .I mail | |
753 | program in the first example above was terminated when we hit a control-d | |
754 | which generates and end-of-file from the standard input. | |
755 | The shell also terminates when it gets an end-of-file printing `logout'; | |
756 | \s-2UNIX\s0 then logs you off the system. | |
757 | Since this means that typing too many control-d's can accidentally log us off, | |
758 | the shell has a mechanism for preventing this. | |
759 | This | |
760 | .I ignoreeof | |
761 | option will be discussed in section 2.2. | |
762 | .PP | |
763 | If a command has its standard input redirected from a file, then it will | |
764 | normally terminate when it reaches the end of this file. | |
765 | Thus if we execute | |
766 | .DS | |
767 | mail bill < prepared.text | |
768 | .DE | |
769 | the mail command will terminate without our typing a control-d. | |
770 | This is because it read to the end-of-file of our file | |
771 | `prepared.text' in which we placed a message for `bill' with an editor. | |
772 | We could also have done | |
773 | .DS | |
774 | cat prepared.text \||\| mail bill | |
775 | .DE | |
776 | since the | |
777 | .I cat | |
778 | command would then have written the text through the pipe to the | |
779 | standard input of the mail command. | |
780 | When the | |
781 | .I cat | |
782 | command completed it would have terminated, | |
783 | closing down the pipeline | |
784 | and the | |
785 | .I mail | |
786 | command would have received an end-of-file from it and terminated. | |
787 | Using a pipe here is more complicated than redirecting input | |
788 | so we would more likely use the first form. | |
789 | These commands could also have been stopped by sending an \s-2INTERRUPT\s0. | |
790 | .PP | |
791 | If you write or run programs which are not fully debugged then it may | |
792 | be necessary to stop them somewhat ungracefully. | |
793 | This can be done by sending them a \s-2QUIT\s0 | |
794 | signal, generated by a control-\e. | |
795 | This will usually provoke the shell to produce a message like: | |
796 | .DS | |
797 | a.out: Quit \-\- Core dumped | |
798 | .DE | |
799 | indicating that a file | |
800 | `core' has been created containing information about the program `a.out's | |
801 | state when it ran amuck. | |
802 | You can examine this file yourself, or forward information to the | |
803 | maintainer of the program telling him/her where the | |
804 | .I "core file" | |
805 | is. | |
806 | .PP | |
807 | If you run background commands (as explained in section 2.6) then these | |
808 | commands will ignore \s-2INTERRUPT\s0 and \s-2QUIT\s0 signals at the | |
809 | terminal. To stop them you must use the | |
810 | .I kill | |
811 | program. See section 2.6 for an example. | |
812 | .PP | |
813 | If you want to examine the output of a command without having it shoot | |
814 | off the screen as the output of the | |
815 | .DS | |
816 | cat /etc/passwd | |
817 | .DE | |
818 | command will, you can use the command | |
819 | .DS | |
820 | more /etc/passwd | |
821 | .DE | |
822 | The | |
823 | .I more | |
824 | program pauses after each complete screenful and types `\-\-More\-\-' | |
825 | at which point you can hit a space to get another screenful, a return | |
826 | to get another line, or a `q' to end the | |
827 | .I more | |
828 | program. You can also use more as a filter, i.e. | |
829 | .DS | |
830 | cat /etc/passwd | more | |
831 | .DE | |
832 | works just like the more simple more command above. | |
833 | .PP | |
834 | For stopping output of commands not involving more you can use the | |
835 | control-s key to stop the typeout. The typeout will resume when you | |
836 | hit control-q or any other key, but control-q is normally used because | |
837 | it only restarts the output and does not become input to the program | |
838 | which is running. This works well on low-speed terminals, but at 9600 | |
839 | baud it is hard to type control-s and control-q fast enough to paginate | |
840 | the output nicely, and a program like | |
841 | .I more | |
842 | is usually used. | |
843 | .NH 2 | |
844 | What now? | |
845 | .PP | |
846 | We have so far seen a number of mechanisms of the shell and learned a lot | |
847 | about the way in which it operates. | |
848 | The remaining sections will go yet further into the internals of the | |
849 | shell, but you will surely want to try using the shell before you go any further. | |
850 | To try it you can log in to \s-2UNIX\s0 and type the following | |
851 | command to the system: | |
852 | .DS | |
853 | chsh myname /bin/csh | |
854 | .DE | |
855 | Here | |
856 | `myname' | |
857 | should be replaced by the name you typed to | |
858 | the system prompt of | |
859 | `login:' to get onto the system. | |
860 | Thus I would use `chsh bill /bin/csh'. | |
861 | .B | |
862 | You only have to do this once; it takes effect at next login. | |
863 | .R | |
864 | You are now ready to try using | |
865 | .I csh. | |
866 | .PP | |
867 | Before you do the `chsh' command, the shell you are using when | |
868 | you log into the system is `/bin/sh'. | |
869 | In fact, much of the above discussion is applicable to `/bin/sh'. | |
870 | The next section will introduce many features particular to | |
871 | .I csh | |
872 | so you should change your shell to | |
873 | .I csh | |
874 | before you begin reading it. | |
875 | .bp |