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