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