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