Commit | Line | Data |
---|---|---|
369b418a 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 | .\" | |
8c63abc7 | 5 | .\" @(#)csh.2 6.1 (Berkeley) %G% |
369b418a KD |
6 | .\" |
7 | .nr H1 1 | |
8 | .NH | |
9 | Details on the shell for terminal users | |
10 | .NH 2 | |
11 | Shell startup and termination | |
12 | .PP | |
13 | When you login, the shell is started by the system in your | |
14 | .I home | |
15 | directory and begins by reading commands from a file | |
16 | .I \&.cshrc | |
17 | in this directory. | |
18 | All shells which you may start during your terminal session will | |
19 | read from this file. | |
20 | We will later see what kinds of commands are usefully placed there. | |
21 | For now we need not have this file and the shell does not complain about | |
22 | its absence. | |
23 | .PP | |
24 | A | |
25 | .I "login shell" , | |
26 | executed after you login to the system, | |
27 | will, after it reads commands from | |
28 | .I \&.cshrc, | |
29 | read commands from a file | |
30 | .I \&.login | |
31 | also in your home directory. | |
32 | This file contains commands which you wish to do each time you login | |
33 | to the \s-2UNIX\s0 system. | |
34 | My | |
35 | .I \&.login | |
36 | file looks something like: | |
37 | .DS | |
38 | set ignoreeof | |
39 | set mail=(/usr/spool/mail/bill) | |
40 | echo "${prompt}users" ; users | |
41 | alias ts \e | |
42 | \'set noglob ; eval \`tset \-s \-m dialup:c100rv4pna \-m plugboard:?hp2621nl \!*\`\'; | |
8c63abc7 | 43 | ts; stty intr ^C kill ^U crt |
369b418a KD |
44 | set time=15 history=10 |
45 | msgs \-f | |
46 | if (\-e $mail) then | |
47 | echo "${prompt}mail" | |
48 | ||
49 | endif | |
50 | .DE | |
51 | .PP | |
52 | This file contains several commands to be executed by \s-2UNIX\s0 | |
53 | each time I login. | |
54 | The first is a | |
55 | .I set | |
56 | command which is interpreted directly by the shell. It sets the shell | |
57 | variable | |
58 | .I ignoreeof | |
8c63abc7 | 59 | which causes the shell to not log me off if I hit ^D. Rather, |
369b418a KD |
60 | I use the |
61 | .I logout | |
62 | command to log off of the system. | |
63 | By setting the | |
64 | .I mail | |
65 | variable, I ask the shell to watch for incoming mail to me. Every 5 minutes | |
66 | the shell looks for this file and tells me if more mail has arrived there. | |
67 | An alternative to this is to put the command | |
68 | .DS | |
69 | biff y | |
70 | .DE | |
71 | in place of this | |
72 | .I set; | |
73 | this will cause me to be notified immediately when mail arrives, and to | |
74 | be shown the first few lines of the new message. | |
75 | .PP | |
76 | Next I set the shell variable `time' to `15' causing the shell to automatically | |
77 | print out statistics lines for commands which execute for at least 15 seconds | |
78 | of \s-2CPU\s+2 time. The variable `history' is set to 10 indicating that | |
79 | I want the shell to remember the last 10 commands I type in its | |
80 | .I "history list" , | |
81 | (described later). | |
82 | .PP | |
83 | I create an | |
84 | .I alias | |
85 | ``ts'' which executes a | |
86 | \fItset\fR\|(1) command setting up the modes of the terminal. | |
87 | The parameters to | |
88 | .I tset | |
89 | indicate the kinds of terminal which I usually use when not on a hardwired | |
90 | port. I then execute ``ts'' and also use the | |
91 | .I stty | |
8c63abc7 KD |
92 | command to change the interrupt character to ^C and the line kill |
93 | character to ^U. | |
369b418a KD |
94 | .PP |
95 | I then run the `msgs' program, which provides me with any | |
96 | system messages which I have not seen before; the `\-f' option here prevents | |
97 | it from telling me anything if there are no new messages. | |
98 | Finally, if my mailbox file exists, then I run the `mail' program to | |
99 | process my mail. | |
100 | .PP | |
101 | When the `mail' and `msgs' programs finish, the shell will finish | |
102 | processing my | |
103 | .I \&.login | |
104 | file and begin reading commands from the terminal, prompting for each with | |
105 | `% '. | |
106 | When I log off (by giving the | |
107 | .I logout | |
108 | command) the shell | |
109 | will print `logout' and execute commands from the file `.logout' | |
110 | if it exists in my home directory. | |
111 | After that the shell will terminate and \s-2UNIX\s0 will log | |
112 | me off the system. | |
113 | If the system is not going down, I will receive a new login message. | |
114 | In any case, after the `logout' message the shell is committed to terminating | |
115 | and will take no further input from my terminal. | |
116 | .NH 2 | |
117 | Shell variables | |
118 | .PP | |
119 | The shell maintains a set of | |
120 | .I variables. | |
121 | We saw above the variables | |
122 | .I history | |
123 | and | |
124 | .I time | |
125 | which had values `10' and `15'. | |
126 | In fact, each shell variable has as value an array of | |
127 | zero or more | |
128 | .I strings. | |
129 | Shell variables may be assigned values by the set command. It has | |
130 | several forms, the most useful of which was given above and is | |
131 | .DS | |
132 | set name=value | |
133 | .DE | |
134 | .PP | |
135 | Shell variables may be used to store values which are to | |
136 | be used in commands later through a substitution mechanism. | |
137 | The shell variables most commonly referenced are, however, those which the | |
138 | shell itself refers to. | |
139 | By changing the values of these variables one can directly affect the | |
140 | behavior of the shell. | |
141 | .PP | |
142 | One of the most important variables is the variable | |
143 | .I path. | |
144 | This variable contains a sequence of directory names where the shell | |
145 | searches for commands. | |
146 | The | |
147 | .I set | |
148 | command with no arguments | |
149 | shows the value of all variables currently defined (we usually say | |
150 | .I set) | |
151 | in the shell. | |
152 | The default value for path will be shown by | |
153 | .I set | |
154 | to be | |
155 | .DS | |
156 | % set | |
157 | .ta .75i | |
158 | argv () | |
159 | cwd /usr/bill | |
160 | home /usr/bill | |
161 | path (. /usr/ucb /bin /usr/bin) | |
162 | prompt % | |
163 | shell /bin/csh | |
164 | status 0 | |
165 | term c100rv4pna | |
166 | user bill | |
167 | % | |
168 | .so tabs | |
169 | .DE | |
170 | This output indicates that the variable path points to the current | |
171 | directory `.' and then `/usr/ucb', `/bin' and `/usr/bin'. | |
172 | Commands which you may write might be in `.' (usually one of | |
173 | your directories). | |
174 | Commands developed at Berkeley, live in `/usr/ucb' | |
175 | while commands developed at Bell Laboratories live in `/bin' and `/usr/bin'. | |
176 | .PP | |
177 | A number of locally developed programs on the system live in the directory | |
178 | `/usr/local'. | |
179 | If we wish that all shells which we invoke to have | |
180 | access to these new programs we can place the command | |
181 | .DS | |
182 | set path=(. /usr/ucb /bin /usr/bin /usr/local) | |
183 | .DE | |
184 | in our file | |
185 | .I \&.cshrc | |
186 | in our home directory. | |
187 | Try doing this and then logging out and back in and do | |
188 | .DS | |
189 | set | |
190 | .DE | |
191 | again to see that the value assigned to | |
192 | .I path | |
193 | has changed. | |
8c63abc7 KD |
194 | .FS \(dg |
195 | Another directory that might interest you is /usr/new, which contains | |
196 | many useful user-contributed programs provided with Berkeley Unix. | |
197 | .FE | |
369b418a KD |
198 | .PP |
199 | One thing you should be aware of is that the shell examines each directory | |
200 | which you insert into your path and determines which commands are contained | |
201 | there. Except for the current directory `.', which the shell treats specially, | |
202 | this means that if commands are added to a directory in your search path after | |
203 | you have started the shell, they will not necessarily be found by the shell. | |
204 | If you wish to use a command which has been added in this way, you should | |
205 | give the command | |
206 | .DS | |
207 | rehash | |
208 | .DE | |
209 | to the shell, which will cause it to recompute its internal table of command | |
210 | locations, so that it will find the newly added command. | |
211 | Since the shell has to look in the current directory `.' on each command, | |
212 | placing it at the end of the path specification usually works equivalently | |
213 | and reduces overhead. | |
214 | .PP | |
215 | Other useful built in variables are the variable | |
216 | .I home | |
217 | which shows your home directory, | |
218 | .I cwd | |
219 | which contains your current working directory, | |
220 | the variable | |
221 | .I ignoreeof | |
222 | which can be set in your | |
223 | .I \&.login | |
224 | file to tell the shell not to exit when it receives an end-of-file from | |
225 | a terminal (as described above). | |
226 | The variable `ignoreeof' | |
227 | is one of several variables which the shell does not care about the | |
228 | value of, only whether they are | |
229 | .I set | |
230 | or | |
231 | .I unset. | |
232 | Thus to set this variable you simply do | |
233 | .DS | |
234 | set ignoreeof | |
235 | .DE | |
236 | and to unset it do | |
237 | .DS | |
238 | unset ignoreeof | |
239 | .DE | |
240 | These give the variable `ignoreeof' no value, but none is desired or required. | |
241 | .PP | |
242 | Finally, some other built-in shell variables of use are the | |
243 | variables | |
244 | .I noclobber | |
245 | and | |
246 | .I mail. | |
247 | The metasyntax | |
248 | .DS | |
249 | > filename | |
250 | .DE | |
251 | which redirects the standard output of a command | |
252 | will overwrite and destroy the previous contents of the named file. | |
253 | In this way you may accidentally overwrite a file which is valuable. | |
254 | If you would prefer that the shell not overwrite files in this | |
255 | way you can | |
256 | .DS | |
257 | set noclobber | |
258 | .DE | |
259 | in your | |
260 | .I \&.login | |
261 | file. | |
262 | Then trying to do | |
263 | .DS | |
264 | date > now | |
265 | .DE | |
266 | would cause a diagnostic if `now' existed already. | |
267 | You could type | |
268 | .DS | |
269 | date >! now | |
270 | .DE | |
271 | if you really wanted to overwrite the contents of `now'. | |
272 | The `>!' is a special metasyntax indicating that clobbering the | |
273 | file is ok.\(dg | |
274 | .FS | |
275 | \(dgThe space between the `!' and the word `now' is critical here, as `!now' | |
276 | would be an invocation of the | |
277 | .I history | |
278 | mechanism, and have a totally different effect. | |
279 | .FE | |
280 | .NH 2 | |
281 | The shell's history list | |
282 | .PP | |
283 | The shell can maintain a | |
284 | .I "history list" | |
285 | into which it places the words | |
286 | of previous commands. | |
287 | It is possible to use a notation to reuse commands or words | |
288 | from commands in forming new commands. | |
289 | This mechanism can be used to repeat previous commands or to | |
290 | correct minor typing mistakes in commands. | |
291 | .PP | |
292 | The following figure gives a sample session involving typical usage of the | |
293 | history mechanism of the shell. | |
294 | .KF | |
295 | .DS | |
296 | % cat bug.c | |
297 | main() | |
298 | ||
299 | { | |
300 | printf("hello); | |
301 | } | |
302 | % cc !$ | |
303 | cc bug.c | |
304 | "bug.c", line 4: newline in string or char constant | |
305 | "bug.c", line 5: syntax error | |
306 | % ed !$ | |
307 | ed bug.c | |
308 | 29 | |
309 | 4s/);/"&/p | |
310 | printf("hello"); | |
311 | w | |
312 | 30 | |
313 | q | |
314 | % !c | |
315 | cc bug.c | |
316 | % a.out | |
317 | hello% !e | |
318 | ed bug.c | |
319 | 30 | |
320 | 4s/lo/lo\e\en/p | |
321 | printf("hello\en"); | |
322 | w | |
323 | 32 | |
324 | q | |
325 | % !c \-o bug | |
326 | cc bug.c \-o bug | |
327 | % size a.out bug | |
328 | a.out: 2784+364+1028 = 4176b = 0x1050b | |
329 | bug: 2784+364+1028 = 4176b = 0x1050b | |
330 | % ls \-l !* | |
331 | ls \-l a.out bug | |
332 | \(mirwxr\(mixr\(mix 1 bill 3932 Dec 19 09:41 a.out | |
333 | \(mirwxr\(mixr\(mix 1 bill 3932 Dec 19 09:42 bug | |
334 | % bug | |
335 | hello | |
336 | % num bug.c | spp | |
337 | spp: Command not found. | |
8c63abc7 | 338 | % ^spp^ssp |
369b418a KD |
339 | num bug.c | ssp |
340 | 1 main() | |
341 | 3 { | |
342 | 4 printf("hello\en"); | |
343 | 5 } | |
344 | % !! | lpr | |
345 | num bug.c | ssp | lpr | |
346 | % | |
347 | .DE | |
348 | .KE | |
349 | In this example we have a very simple C program which has a bug (or two) | |
350 | in it in the file `bug.c', which we `cat' out on our terminal. We then | |
351 | try to run the C compiler on it, referring to the file again as `!$', | |
352 | meaning the last argument to the previous command. Here the `!' is the | |
353 | history mechanism invocation metacharacter, and the `$' stands for the last | |
354 | argument, by analogy to `$' in the editor which stands for the end of the line. | |
355 | The shell echoed the command, as it would have been typed without use of | |
356 | the history mechanism, and then executed it. | |
357 | The compilation yielded error diagnostics so we now run the editor on the | |
358 | file we were trying to compile, fix the bug, and run the C compiler again, | |
359 | this time referring to this command simply as `!c', which repeats the last | |
360 | command which started with the letter `c'. If there were other | |
361 | commands starting with `c' done recently we could have said `!cc' or even | |
362 | `!cc:p' which would have printed the last command starting with `cc' | |
363 | without executing it. | |
364 | .PP | |
365 | After this recompilation, we ran the resulting `a.out' file, and then | |
366 | noting that there still was a bug, ran the editor again. After fixing | |
367 | the program we ran the C compiler again, but tacked onto the command | |
368 | an extra `\-o bug' telling the compiler to place the resultant binary in | |
369 | the file `bug' rather than `a.out'. In general, the history mechanisms | |
370 | may be used anywhere in the formation of new commands and other characters | |
371 | may be placed before and after the substituted commands. | |
372 | .PP | |
373 | We then ran the `size' command to see how large the binary program images | |
374 | we have created were, and then an `ls \-l' command with the same argument | |
375 | list, denoting the argument list `\!*'. | |
376 | Finally we ran the program `bug' to see that its output is indeed correct. | |
377 | .PP | |
378 | To make a numbered listing of the program we ran the `num' command on the file `bug.c'. | |
379 | In order to compress out blank lines in the output of `num' we ran the | |
380 | output through the filter `ssp', but misspelled it as spp. To correct this | |
8c63abc7 | 381 | we used a shell substitute, placing the old text and new text between `^' |
369b418a KD |
382 | characters. This is similar to the substitute command in the editor. |
383 | Finally, we repeated the same command with `!!', but sent its output to the | |
384 | line printer. | |
385 | .PP | |
386 | There are other mechanisms available for repeating commands. The | |
387 | .I history | |
388 | command prints out a number of previous commands with numbers by which | |
389 | they can be referenced. There is a way to refer to a previous command | |
390 | by searching for a string which appeared in it, and there are other, | |
391 | less useful, ways to select arguments to include in a new command. | |
392 | A complete description of all these mechanisms | |
8c63abc7 | 393 | is given in the C shell manual pages in the \s-2UNIX\s0 Programmer's Manual. |
369b418a KD |
394 | .NH 2 |
395 | Aliases | |
396 | .PP | |
397 | The shell has an | |
398 | .I alias | |
399 | mechanism which can be used to make transformations on input commands. | |
400 | This mechanism can be used to simplify the commands you type, | |
401 | to supply default arguments to commands, | |
402 | or to perform transformations on commands and their arguments. | |
403 | The alias facility is similar to a macro facility. | |
404 | Some of the features obtained by aliasing can be obtained also | |
405 | using shell command files, but these take place in another instance | |
406 | of the shell and cannot directly affect the current shells environment | |
407 | or involve commands such as | |
408 | .I cd | |
409 | which must be done in the current shell. | |
410 | .PP | |
411 | As an example, suppose that there is a new version of the mail program | |
412 | on the system called `newmail' | |
413 | you wish to use, rather than the standard mail program which is called | |
414 | `mail'. | |
415 | If you place the shell command | |
416 | .DS | |
417 | alias mail newmail | |
418 | .DE | |
419 | in your | |
420 | .I \&.cshrc | |
421 | file, the shell will transform an input line of the form | |
422 | .DS | |
423 | mail bill | |
424 | .DE | |
425 | into a call on `newmail'. | |
426 | More generally, suppose we wish the command `ls' to always show | |
427 | sizes of files, that is to always do `\-s'. | |
428 | We can do | |
429 | .DS | |
430 | alias ls ls \-s | |
431 | .DE | |
432 | or even | |
433 | .DS | |
434 | alias dir ls \-s | |
435 | .DE | |
436 | creating a new command syntax `dir' | |
437 | which does an `ls \-s'. | |
438 | If we say | |
439 | .DS | |
440 | dir ~bill | |
441 | .DE | |
442 | then the shell will translate this to | |
443 | .DS | |
444 | ls \-s /mnt/bill | |
445 | .DE | |
446 | .PP | |
447 | Thus the | |
448 | .I alias | |
449 | mechanism can be used to provide short names for commands, | |
450 | to provide default arguments, | |
451 | and to define new short commands in terms of other commands. | |
452 | It is also possible to define aliases which contain multiple | |
453 | commands or pipelines, showing where the arguments to the original | |
454 | command are to be substituted using the facilities of the | |
455 | history mechanism. | |
456 | Thus the definition | |
457 | .DS | |
458 | alias cd \'cd \e!* ; ls \' | |
459 | .DE | |
460 | would do an | |
461 | .I ls | |
462 | command after each change directory | |
463 | .I cd | |
464 | command. | |
465 | We enclosed the entire alias definition in `\'' characters to prevent | |
466 | most substitutions from occurring and the character `;' from being | |
467 | recognized as a metacharacter. | |
468 | The `!' here is escaped with a `\e' to prevent it from being interpreted | |
469 | when the alias command is typed in. | |
470 | The `\e!*' here substitutes the entire argument list to the pre-aliasing | |
471 | .I cd | |
472 | command, without giving an error if there were no arguments. | |
473 | The `;' separating commands is used here | |
474 | to indicate that one command is to be done and then the next. | |
475 | Similarly the definition | |
476 | .DS | |
8c63abc7 | 477 | alias whois \'grep \e!^ /etc/passwd\' |
369b418a KD |
478 | .DE |
479 | defines a command which looks up its first argument in the password file. | |
480 | .PP | |
481 | .B Warning: | |
482 | The shell currently reads the | |
483 | .I \&.cshrc | |
484 | file each time it starts up. If you place a large number of commands | |
485 | there, shells will tend to start slowly. A mechanism for saving the shell | |
486 | environment after reading the \fI\&.cshrc\fR file and quickly restoring it is | |
487 | under development, but for now you should try to limit the number of | |
488 | aliases you have to a reasonable number... 10 or 15 is reasonable, | |
489 | 50 or 60 will cause a noticeable delay in starting up shells, and make | |
490 | the system seem sluggish when you execute commands from within the editor | |
491 | and other programs. | |
492 | .NH 2 | |
493 | More redirection; >> and >& | |
494 | .PP | |
495 | There are a few more notations useful to the terminal user | |
496 | which have not been introduced yet. | |
497 | .PP | |
498 | In addition to the standard output, commands also have a | |
499 | .I "diagnostic output" | |
500 | which is normally directed to the terminal even when the standard output | |
501 | is redirected to a file or a pipe. | |
502 | It is occasionally desirable to direct the diagnostic output along with | |
503 | the standard output. | |
504 | For instance if you want to redirect the output of a long running command | |
505 | into a file and wish to have a record of any error diagnostic it produces | |
506 | you can do | |
507 | .DS | |
508 | command >& file | |
509 | .DE | |
510 | The `>&' here tells the shell to route both the diagnostic output and the | |
511 | standard output into `file'. | |
512 | Similarly you can give the command | |
513 | .DS | |
514 | command |\|& lpr | |
515 | .DE | |
516 | to route both standard and diagnostic output through the pipe | |
517 | to the line printer daemon | |
8c63abc7 | 518 | .I lpr.\(dd |
369b418a | 519 | .FS |
8c63abc7 KD |
520 | \(dd A command of the form |
521 | .br | |
369b418a KD |
522 | .ti +5 |
523 | command >&! file | |
8c63abc7 | 524 | .br |
369b418a KD |
525 | exists, and is used when |
526 | .I noclobber | |
527 | is set and | |
528 | .I file | |
529 | already exists. | |
530 | .FE | |
531 | .PP | |
532 | Finally, it is possible to use the form | |
533 | .DS | |
534 | command >> file | |
535 | .DE | |
536 | to place output at the end of an existing file.\(dg | |
537 | .FS | |
8c63abc7 | 538 | \(dg If |
369b418a KD |
539 | .I noclobber |
540 | is set, then an error will result if | |
541 | .I file | |
542 | does not exist, otherwise the shell will create | |
543 | .I file | |
544 | if it doesn't exist. | |
545 | A form | |
8c63abc7 | 546 | .br |
369b418a KD |
547 | .ti +5 |
548 | command >>! file | |
8c63abc7 | 549 | .br |
369b418a KD |
550 | makes it not be an error for file to not exist when |
551 | .I noclobber | |
552 | is set. | |
553 | .FE | |
554 | .NH 2 | |
555 | Jobs; Background, Foreground, or Suspended | |
556 | .PP | |
557 | When one or more commands | |
558 | are typed together as a pipeline or as a sequence of commands separated by | |
559 | semicolons, a single | |
560 | .I job | |
561 | is created by the shell consisting of these commands together as a unit. | |
562 | Single commands without pipes or semicolons create the simplest jobs. | |
563 | Usually, every line typed to the shell creates a job. | |
564 | Some lines that create jobs (one per line) are | |
565 | .DS | |
566 | sort < data | |
567 | ls \-s | sort \-n | head \-5 | |
568 | mail harold | |
569 | .DE | |
570 | .PP | |
571 | If the metacharacter `&' is typed | |
572 | at the end of the commands, then the job is started as a | |
573 | .I background | |
574 | job. This means that the shell does not wait for it to complete but | |
575 | immediately prompts and is ready for another command. The job runs | |
576 | .I "in the background" | |
577 | at the same time that normal jobs, called | |
578 | .I foreground | |
579 | jobs, continue to be read and executed by the shell one at a time. | |
580 | Thus | |
581 | .DS | |
582 | du > usage & | |
583 | .DE | |
584 | would run the | |
585 | .I du | |
586 | program, which reports on the disk usage of your working directory (as well as | |
587 | any directories below it), put the output into the file `usage' and return | |
588 | immediately with a prompt for the next command without out waiting for | |
589 | .I du | |
590 | to finish. The | |
591 | .I du | |
592 | program would continue executing in the background | |
593 | until it finished, even though you can type and execute more commands in the | |
594 | mean time. | |
595 | When a background | |
596 | job terminates, a message is typed by the shell just before the next prompt | |
597 | telling you that the job has completed. | |
598 | In the following example the | |
599 | .I du | |
600 | job finishes sometime during the | |
601 | execution of the | |
602 | .I mail | |
603 | command and its completion is reported just before | |
604 | the prompt after the | |
605 | .I mail | |
606 | job is finished. | |
607 | .DS | |
608 | % du > usage & | |
609 | [1] 503 | |
610 | % mail bill | |
611 | How do you know when a background job is finished? | |
612 | EOT | |
613 | .ta 1.75i | |
614 | [1] \- Done du > usage | |
615 | % | |
616 | .so tabs | |
617 | .DE | |
618 | If the job did not terminate normally the `Done' message might say | |
619 | something else like `Killed'. | |
620 | If you want the | |
621 | terminations of background jobs to be reported at the time they occur | |
622 | (possibly interrupting the output of other foreground jobs), you can set | |
623 | the | |
624 | .I notify | |
625 | variable. In the previous example this would mean that the | |
626 | `Done' message might have come right in the middle of the message to | |
627 | Bill. | |
628 | Background jobs are unaffected by any signals from the keyboard like | |
629 | the \s-2STOP\s0, \s-2INTERRUPT\s0, or \s-2QUIT\s0 signals mentioned earlier. | |
630 | .PP | |
631 | Jobs are recorded in a table inside the shell until they terminate. | |
632 | In this table, the shell remembers the command names, arguments and the | |
633 | .I "process numbers" | |
634 | of all commands in the job as well as the working directory where the job was | |
635 | started. | |
636 | Each job in the table is either running | |
637 | .I "in the foreground" | |
638 | with the shell waiting for it to terminate, running | |
639 | .I "in the background," | |
640 | or | |
641 | .I suspended. | |
642 | Only one job can be running in the foreground at one time, but several | |
643 | jobs can be suspended or running in the background at once. As each job | |
644 | is started, it is assigned a small identifying | |
645 | number called the | |
646 | .I "job number" | |
647 | which can be used later to refer to the job in the commands described below. | |
648 | Job numbers remain | |
649 | the same until the job terminates and then are re-used. | |
650 | .PP | |
651 | When a job is started in the backgound using `&', its number, as well | |
652 | as the process numbers of all its (top level) commands, is typed by the shell | |
653 | before prompting you for another command. For example, | |
654 | .DS | |
655 | % ls \-s | sort \-n > usage & | |
656 | [2] 2034 2035 | |
657 | % | |
658 | .DE | |
659 | runs the `ls' program with the `\-s' options, pipes this output into | |
660 | the `sort' program with the `\-n' option which puts its output into the | |
661 | file `usage'. | |
662 | Since the `&' was at the end of the line, these two programs were started | |
663 | together as a background job. After starting the job, the shell prints | |
664 | the job number in brackets (2 in this case) followed by the process number | |
665 | of each program started in the job. Then the shell immediates prompts for | |
666 | a new command, leaving the job running simultaneously. | |
667 | .PP | |
668 | As mentioned in section 1.8, foreground jobs become | |
669 | .I suspended | |
8c63abc7 | 670 | by typing ^Z |
369b418a KD |
671 | which sends a \s-2STOP\s0 signal to the currently running |
672 | foreground job. A background job can become suspended by using the | |
673 | .I stop | |
674 | command described below. When jobs are suspended they merely stop | |
675 | any further progress until started again, either in the foreground | |
676 | or the backgound. The shell notices when a job becomes stopped and | |
677 | reports this fact, much like it reports the termination of background jobs. | |
678 | For foreground jobs this looks like | |
679 | .DS | |
680 | % du > usage | |
8c63abc7 | 681 | ^Z |
369b418a KD |
682 | Stopped |
683 | % | |
684 | .DE | |
685 | `Stopped' message is typed by the shell when it notices that the | |
686 | .I du | |
687 | program stopped. | |
688 | For background jobs, using the | |
689 | .I stop | |
690 | command, it is | |
691 | .DS | |
692 | % sort usage & | |
693 | [1] 2345 | |
694 | % stop %1 | |
695 | .ta 1.75i | |
696 | [1] + Stopped (signal) sort usage | |
697 | % | |
698 | .so tabs | |
699 | .DE | |
700 | Suspending foreground jobs can be very useful when you need to temporarily | |
701 | change what you are doing (execute other commands) and then return to | |
702 | the suspended job. Also, foreground jobs can be suspended and then | |
703 | continued as background jobs using the | |
704 | .I bg | |
705 | command, allowing you to continue other work and | |
706 | stop waiting for the foreground job to finish. Thus | |
707 | .DS | |
708 | % du > usage | |
8c63abc7 | 709 | ^Z |
369b418a KD |
710 | Stopped |
711 | % bg | |
712 | [1] du > usage & | |
713 | % | |
714 | .DE | |
715 | starts `du' in the foreground, stops it before it finishes, then continues | |
716 | it in the background allowing more foreground commands to be executed. | |
717 | This is especially helpful | |
718 | when a foreground job ends up taking longer than you expected and you | |
719 | wish you had started it in the backgound in the beginning. | |
720 | .PP | |
721 | All | |
722 | .I "job control" | |
723 | commands can take an argument that identifies a particular | |
724 | job. | |
725 | All job name arguments begin with the character `%', since some of the | |
726 | job control commands also accept process numbers (printed by the | |
727 | .I ps | |
728 | command.) | |
729 | The default job (when no argument is given) is called the | |
730 | .I current | |
731 | job and is identified by a `+' in the output of the | |
732 | .I jobs | |
733 | command, which shows you which jobs you have. | |
734 | When only one job is stopped or running in the background (the usual case) | |
735 | it is always the current job thus no argument is needed. | |
736 | If a job is stopped while running in the foreground it becomes the | |
737 | .I current | |
738 | job and the existing current job becomes the | |
739 | .I previous | |
740 | job \- identified by a `\-' in the output of | |
741 | .I jobs. | |
742 | When the current job terminates, the previous job becomes the current job. | |
743 | When given, the argument is either `%\-' (indicating | |
744 | the previous job); `%#', where # is the job number; | |
745 | `%pref' where pref is some unique prefix of the command name | |
746 | and arguments of one of the jobs; or `%?' followed by some string found | |
747 | in only one of the jobs. | |
748 | .PP | |
749 | The | |
750 | .I jobs | |
751 | command types the table of jobs, giving the job number, | |
752 | commands and status (`Stopped' or `Running') of each backgound or | |
753 | suspended job. With the `\-l' option the process numbers are also | |
754 | typed. | |
755 | .DS | |
756 | % du > usage & | |
757 | [1] 3398 | |
758 | % ls \-s | sort \-n > myfile & | |
759 | [2] 3405 | |
760 | % mail bill | |
8c63abc7 | 761 | ^Z |
369b418a KD |
762 | Stopped |
763 | % jobs | |
764 | .ta 1.75i | |
765 | [1] \(mi Running du > usage | |
766 | [2] Running ls \-s | sort \-n > myfile | |
767 | [3] \(pl Stopped mail bill | |
768 | % fg %ls | |
769 | ls \-s | sort \-n > myfile | |
770 | % more myfile | |
771 | .so tabs | |
772 | .DE | |
773 | .PP | |
774 | The | |
775 | .I fg | |
776 | command runs a suspended or background job in the foreground. It is | |
777 | used to restart a previously suspended job or change a background job | |
778 | to run in the foreground (allowing signals or input from the terminal). | |
779 | In the above example we used | |
780 | .I fg | |
781 | to change the `ls' job from the | |
782 | background to the foreground since we wanted to wait for it to | |
783 | finish before looking at its output file. | |
784 | The | |
785 | .I bg | |
786 | command runs a suspended job in the background. It is usually used | |
787 | after stopping the currently running foreground job with the | |
788 | \s-2STOP\s0 signal. The combination of the \s-2STOP\s0 signal and the | |
789 | .I bg | |
790 | command changes a foreground job into a background job. | |
791 | The | |
792 | .I stop | |
793 | command suspends a background job. | |
794 | .PP | |
795 | The | |
796 | .I kill | |
797 | command terminates a background or suspended job immediately. | |
798 | In addition to jobs, it may be given process numbers as arguments, | |
799 | as printed by | |
800 | .I ps. | |
801 | Thus, in the example above, the running | |
802 | .I du | |
803 | command could have been terminated by the command | |
804 | .DS | |
805 | % kill %1 | |
806 | .ta 1.75i | |
807 | [1] Terminated du > usage | |
808 | % | |
809 | .so tabs | |
810 | .DE | |
811 | .PP | |
812 | The | |
813 | .I notify | |
814 | command (not the variable mentioned earlier) indicates that the termination | |
815 | of a specific job should be | |
816 | reported at the time it finishes instead of waiting for the next prompt. | |
817 | .PP | |
818 | If a job running in the background tries to read input from the terminal | |
819 | it is automatically stopped. When such a job is then run in the | |
820 | foreground, input can be given to the job. If desired, the job can | |
821 | be run in the background again until it requests input again. | |
822 | This is illustrated in the following sequence where the `s' command in the | |
823 | text editor might take a long time. | |
824 | .ID | |
825 | .nf | |
826 | % ed bigfile | |
827 | 120000 | |
828 | 1,$s/thisword/thatword/ | |
8c63abc7 | 829 | ^Z |
369b418a KD |
830 | Stopped |
831 | % bg | |
832 | [1] ed bigfile & | |
833 | % | |
834 | . . . some foreground commands | |
835 | .ta 1.75i | |
836 | [1] Stopped (tty input) ed bigfile | |
837 | % fg | |
838 | ed bigfile | |
839 | w | |
840 | 120000 | |
841 | q | |
842 | % | |
843 | .so tabs | |
844 | .DE | |
8c63abc7 | 845 | So after the `s' command was issued, the `ed' job was stopped with ^Z |
369b418a KD |
846 | and then put in the background using |
847 | .I bg. | |
848 | Some time later when the `s' command was finished, | |
849 | .I ed | |
850 | tried to read another command and was stopped because jobs | |
851 | in the backgound cannot read from the terminal. The | |
852 | .I fg | |
853 | command returned the `ed' job to the foreground where it could once again | |
854 | accept commands from the terminal. | |
855 | .PP | |
856 | The command | |
857 | .DS | |
858 | stty tostop | |
859 | .DE | |
860 | causes all background jobs run on your terminal to stop | |
861 | when they are about to | |
862 | write output to the terminal. This prevents messages from background | |
863 | jobs from interrupting foreground job output and allows you to run | |
864 | a job in the background without losing terminal output. It also | |
865 | can be used for interactive programs that sometimes have long | |
866 | periods without interaction. Thus each time it outputs a prompt for more | |
867 | input it will stop before the prompt. It can then be run in the | |
868 | foreground using | |
869 | .I fg, | |
870 | more input can be given and, if necessary stopped and returned to | |
871 | the background. This | |
872 | .I stty | |
873 | command might be a good thing to put in your | |
874 | .I \&.login | |
875 | file if you do not like output from background jobs interrupting | |
876 | your work. It also can reduce the need for redirecting the output | |
877 | of background jobs if the output is not very big: | |
878 | .DS | |
879 | % stty tostop | |
880 | % wc hugefile & | |
881 | [1] 10387 | |
882 | % ed text | |
883 | \&. . . some time later | |
884 | q | |
885 | .ta 1.75i | |
886 | [1] Stopped (tty output) wc hugefile | |
887 | % fg wc | |
888 | wc hugefile | |
889 | 13371 30123 302577 | |
890 | % stty \-tostop | |
891 | .so tabs | |
892 | .DE | |
893 | Thus after some time the `wc' command, which counts the lines, words | |
894 | and characters in a file, had one line of output. When it tried to | |
895 | write this to the terminal it stopped. By restarting it in the | |
896 | foreground we allowed it to write on the terminal exactly when we were | |
897 | ready to look at its output. | |
898 | Programs which attempt to change the mode of the terminal will also | |
899 | block, whether or not | |
900 | .I tostop | |
901 | is set, when they are not in the foreground, as | |
902 | it would be very unpleasant to have a background job change the state | |
903 | of the terminal. | |
904 | .PP | |
905 | Since the | |
906 | .I jobs | |
907 | command only prints jobs started in the currently executing shell, | |
908 | it knows nothing about background jobs started in other login sessions | |
909 | or within shell files. The | |
910 | .I ps | |
911 | can be used in this case to find out about background jobs not started | |
912 | in the current shell. | |
913 | .NH 2 | |
914 | Working Directories | |
915 | .PP | |
916 | As mentioned in section 1.6, the shell is always in a particular | |
917 | .I "working directory." | |
918 | The `change directory' command | |
919 | .I chdir | |
920 | (its | |
921 | short form | |
922 | .I cd | |
923 | may also be used) | |
924 | changes the working directory of the shell, | |
925 | that is, changes the directory you | |
926 | are located in. | |
927 | .PP | |
928 | It is useful to make a directory for each project you wish to work on | |
929 | and to place all files related to that project in that directory. | |
930 | The `make directory' command, | |
931 | .I mkdir, | |
932 | creates a new directory. | |
933 | The | |
934 | .I pwd | |
935 | (`print working directory') command | |
936 | reports the absolute pathname of the working directory of the shell, | |
937 | that is, the directory you are | |
938 | located in. | |
939 | Thus in the example below: | |
940 | .DS | |
941 | % pwd | |
942 | /usr/bill | |
943 | % mkdir newpaper | |
944 | % chdir newpaper | |
945 | % pwd | |
946 | /usr/bill/newpaper | |
947 | % | |
948 | .DE | |
949 | the user has created and moved to the | |
950 | directory | |
951 | .I newpaper. | |
952 | where, for example, he might | |
953 | place a group of related files. | |
954 | .PP | |
955 | No matter where you have moved to in a directory hierarchy, | |
956 | you can return to your `home' login directory by doing just | |
957 | .DS | |
958 | cd | |
959 | .DE | |
960 | with no arguments. | |
961 | The name `..' always means the directory above the current one in | |
962 | the hierarchy, thus | |
963 | .DS | |
964 | cd .. | |
965 | .DE | |
966 | changes the shell's working directory to the one directly above the | |
967 | current one. | |
968 | The name `..' can be used in any | |
969 | pathname, thus, | |
970 | .DS | |
971 | cd ../programs | |
972 | .DE | |
973 | means | |
974 | change to the directory `programs' contained in the directory | |
975 | above the current one. | |
976 | If you have several directories for different | |
977 | projects under, say, your home directory, | |
978 | this shorthand notation | |
979 | permits you to switch easily between them. | |
980 | .PP | |
981 | The shell always remembers the pathname of its current working directory in | |
982 | the variable | |
983 | .I cwd. | |
984 | The shell can also be requested to remember the previous directory when | |
985 | you change to a new working directory. If the `push directory' command | |
986 | .I pushd | |
987 | is used in place of the | |
988 | .I cd | |
989 | command, the shell saves the name of the current working directory | |
990 | on a | |
991 | .I "directory stack" | |
992 | before changing to the new one. | |
993 | You can see this list at any time by typing the `directories' | |
994 | command | |
995 | .I dirs. | |
996 | .ID | |
997 | .nf | |
998 | % pushd newpaper/references | |
999 | ~/newpaper/references ~ | |
1000 | % pushd /usr/lib/tmac | |
1001 | /usr/lib/tmac ~/newpaper/references ~ | |
1002 | % dirs | |
1003 | /usr/lib/tmac ~/newpaper/references ~ | |
1004 | % popd | |
1005 | ~/newpaper/references ~ | |
1006 | % popd | |
1007 | ~ | |
1008 | % | |
1009 | .DE | |
1010 | The list is printed in a horizontal line, reading left to right, | |
1011 | with a tilde (~) as | |
1012 | shorthand for your home directory\(emin this case `/usr/bill'. | |
1013 | The directory stack is printed whenever there is more than one | |
1014 | entry on it and it changes. | |
1015 | It is also printed by a | |
1016 | .I dirs | |
1017 | command. | |
1018 | .I Dirs | |
1019 | is usually faster and more informative than | |
1020 | .I pwd | |
1021 | since it shows the current working directory as well as any | |
1022 | other directories remembered in the stack. | |
1023 | .PP | |
1024 | The | |
1025 | .I pushd | |
1026 | command with no argument | |
1027 | alternates the current directory with the first directory in the | |
1028 | list. | |
1029 | The `pop directory' | |
1030 | .I popd | |
1031 | command without an argument returns you to the directory you were in prior to | |
1032 | the current one, discarding the previous current directory from the | |
1033 | stack (forgetting it). | |
1034 | Typing | |
1035 | .I popd | |
1036 | several times in a series takes you backward through the directories | |
1037 | you had been in (changed to) by | |
1038 | .I pushd | |
1039 | command. | |
1040 | There are other options to | |
1041 | .I pushd | |
1042 | and | |
1043 | .I popd | |
1044 | to manipulate the contents of the directory stack and to change | |
1045 | to directories not at the top of the stack; see the | |
1046 | .I csh | |
1047 | manual page for details. | |
1048 | .PP | |
1049 | Since the shell remembers the working directory in which each job | |
1050 | was started, it warns you when you might be confused by restarting | |
1051 | a job in the foreground which has a different working directory than the | |
1052 | current working directory of the shell. Thus if you start a background | |
1053 | job, then change the shell's working directory and then cause the | |
1054 | background job to run in the foreground, the shell warns you that the | |
1055 | working directory of the currently running foreground job is different | |
1056 | from that of the shell. | |
1057 | .DS | |
1058 | % dirs \-l | |
1059 | /mnt/bill | |
1060 | % cd myproject | |
1061 | % dirs | |
1062 | ~/myproject | |
1063 | % ed prog.c | |
1064 | 1143 | |
8c63abc7 | 1065 | ^Z |
369b418a KD |
1066 | Stopped |
1067 | % cd .. | |
1068 | % ls | |
1069 | myproject | |
1070 | textfile | |
1071 | % fg | |
1072 | ed prog.c (wd: ~/myproject) | |
1073 | .DE | |
1074 | This way the shell warns you when there | |
1075 | is an implied change of working directory, even though no cd command was | |
1076 | issued. In the above example the `ed' job was still in `/mnt/bill/project' | |
1077 | even though the shell had changed to `/mnt/bill'. | |
1078 | A similar warning is given when such a foreground job | |
1079 | terminates or is suspended (using the \s-2STOP\s0 signal) since | |
1080 | the return to the shell again implies a change of working directory. | |
1081 | .DS | |
1082 | % fg | |
1083 | ed prog.c (wd: ~/myproject) | |
1084 | . . . after some editing | |
1085 | q | |
1086 | (wd now: ~) | |
1087 | % | |
1088 | .DE | |
1089 | These messages are sometimes confusing if you use programs that change | |
1090 | their own working directories, since the shell only remembers which | |
1091 | directory a job is started in, and assumes it stays there. | |
1092 | The `\-l' option of | |
1093 | .I jobs | |
1094 | will type the working directory | |
1095 | of suspended or background jobs when it is different | |
1096 | from the current working directory of the shell. | |
1097 | .NH 2 | |
1098 | Useful built-in commands | |
1099 | .PP | |
1100 | We now give a few of the useful built-in commands of the shell describing | |
1101 | how they are used. | |
1102 | .PP | |
1103 | The | |
1104 | .I alias | |
1105 | command described above is used to assign new aliases and to show the | |
1106 | existing aliases. | |
1107 | With no arguments it prints the current aliases. | |
1108 | It may also be given only one argument such as | |
1109 | .DS | |
1110 | alias ls | |
1111 | .DE | |
1112 | to show the current alias for, e.g., `ls'. | |
1113 | .PP | |
1114 | The | |
1115 | .I echo | |
1116 | command prints its arguments. | |
1117 | It is often used in | |
1118 | .I "shell scripts" | |
1119 | or as an interactive command | |
1120 | to see what filename expansions will produce. | |
1121 | .PP | |
1122 | The | |
1123 | .I history | |
1124 | command will show the contents of the history list. | |
1125 | The numbers given with the history events can be used to reference | |
1126 | previous events which are difficult to reference using the | |
1127 | contextual mechanisms introduced above. | |
1128 | There is also a shell variable called | |
1129 | .I prompt. | |
1130 | By placing a `!' character in its value the shell will there substitute | |
1131 | the number of the current command in the history list. | |
1132 | You can use this number to refer to this command in a history substitution. | |
1133 | Thus you could | |
1134 | .DS | |
1135 | set prompt=\'\e! % \' | |
1136 | .DE | |
1137 | Note that the `!' character had to be | |
1138 | .I escaped | |
1139 | here even within `\'' characters. | |
1140 | .PP | |
1141 | The | |
1142 | .I limit | |
1143 | command is used to restrict use of resources. | |
1144 | With no arguments it prints the current limitations: | |
1145 | .DS | |
1146 | .ta 1i | |
1147 | cputime unlimited | |
1148 | filesize unlimited | |
1149 | datasize 5616 kbytes | |
1150 | stacksize 512 kbytes | |
1151 | coredumpsize unlimited | |
1152 | .so tabs | |
1153 | .DE | |
1154 | Limits can be set, e.g.: | |
1155 | .DS | |
1156 | limit coredumpsize 128k | |
1157 | .DE | |
1158 | Most reasonable units abbreviations will work; see the | |
1159 | .I csh | |
1160 | manual page for more details. | |
1161 | .PP | |
1162 | The | |
1163 | .I logout | |
1164 | command can be used to terminate a login shell which has | |
1165 | .I ignoreeof | |
1166 | set. | |
1167 | .PP | |
1168 | The | |
1169 | .I rehash | |
1170 | command causes the shell to recompute a table of where commands are | |
1171 | located. This is necessary if you add a command to a directory | |
1172 | in the current shell's search path and wish the shell to find it, | |
1173 | since otherwise the hashing algorithm may tell the shell that the | |
1174 | command wasn't in that directory when the hash table was computed. | |
1175 | .PP | |
1176 | The | |
1177 | .I repeat | |
1178 | command can be used to repeat a command several times. | |
1179 | Thus to make 5 copies of the file | |
1180 | .I one | |
1181 | in the file | |
1182 | .I five | |
1183 | you could do | |
1184 | .DS | |
1185 | repeat 5 cat one >> five | |
1186 | .DE | |
1187 | .PP | |
1188 | The | |
1189 | .I setenv | |
1190 | command can be used | |
1191 | to set variables in the environment. | |
1192 | Thus | |
1193 | .DS | |
1194 | setenv TERM adm3a | |
1195 | .DE | |
1196 | will set the value of the environment variable \s-2TERM\s0 | |
1197 | to | |
1198 | `adm3a'. | |
1199 | A user program | |
1200 | .I printenv | |
1201 | exists which will print out the environment. | |
1202 | It might then show: | |
1203 | .DS | |
1204 | % printenv | |
1205 | HOME=/usr/bill | |
1206 | SHELL=/bin/csh | |
1207 | PATH=:/usr/ucb:/bin:/usr/bin:/usr/local | |
1208 | TERM=adm3a | |
1209 | USER=bill | |
1210 | % | |
1211 | .DE | |
1212 | .PP | |
1213 | The | |
1214 | .I source | |
1215 | command can be used to force the current shell to read commands from | |
1216 | a file. | |
1217 | Thus | |
1218 | .DS | |
1219 | source .cshrc | |
1220 | .DE | |
1221 | can be used after editing in a change to the | |
1222 | .I \&.cshrc | |
8c63abc7 | 1223 | file which you wish to take effect right away. |
369b418a KD |
1224 | .PP |
1225 | The | |
1226 | .I time | |
1227 | command can be used to cause a command to be timed no matter how much | |
1228 | \s-2CPU\s0 time it takes. | |
1229 | Thus | |
1230 | .DS | |
1231 | % time cp /etc/rc /usr/bill/rc | |
1232 | 0.0u 0.1s 0:01 8% 2+1k 3+2io 1pf+0w | |
1233 | % time wc /etc/rc /usr/bill/rc | |
1234 | 52 178 1347 /etc/rc | |
1235 | 52 178 1347 /usr/bill/rc | |
1236 | 104 356 2694 total | |
1237 | 0.1u 0.1s 0:00 13% 3+3k 5+3io 7pf+0w | |
1238 | % | |
1239 | .DE | |
1240 | indicates that the | |
1241 | .I cp | |
1242 | command used a negligible amount of user time (u) | |
1243 | and about 1/10th of a system time (s); the elapsed time was 1 second (0:01), | |
1244 | there was an average memory usage of 2k bytes of program space and 1k | |
1245 | bytes of data space over the cpu time involved (2+1k); the program | |
1246 | did three disk reads and two disk writes (3+2io), and took one page fault | |
1247 | and was not swapped (1pf+0w). | |
1248 | The word count command | |
1249 | .I wc | |
1250 | on the other hand used 0.1 seconds of user time and 0.1 seconds of system | |
1251 | time in less than a second of elapsed time. | |
1252 | The percentage `13%' indicates that over the period when it was active | |
1253 | the command `wc' used an average of 13 percent of the available \s-2CPU\s0 | |
1254 | cycles of the machine. | |
1255 | .PP | |
1256 | The | |
1257 | .I unalias | |
1258 | and | |
1259 | .I unset | |
1260 | commands can be used | |
1261 | to remove aliases and variable definitions from the shell, and | |
1262 | .I unsetenv | |
1263 | removes variables from the environment. | |
1264 | .NH 2 | |
1265 | What else? | |
1266 | .PP | |
1267 | This concludes the basic discussion of the shell for terminal users. | |
1268 | There are more features of the shell to be discussed here, and all | |
1269 | features of the shell are discussed in its manual pages. | |
1270 | One useful feature which is discussed later is the | |
1271 | .I foreach | |
1272 | built-in command which can be used to run the same command | |
1273 | sequence with a number of different arguments. | |
1274 | .PP | |
1275 | If you intend to use \s-2UNIX\s0 a lot you you should look through | |
8c63abc7 | 1276 | the rest of this document and the csh manual pages (section1) to become familiar |
369b418a KD |
1277 | with the other facilities which are available to you. |
1278 | .bp |