BSD 2 development
[unix-history] / doc / csh
CommitLineData
47d38f87
BJ
1.RP
2.TL
3An introduction to the C shell
4.AU
5William Joy
6.AI
7Computer Science Division
8.br
9Department of Electrical Engineering and Computer Science
10.br
11University of California, Berkeley
12.br
13Berkeley, California 94720
14.AB
15.I Csh
16is a new command language interpreter for \s-2UNIX\s0
17systems.
18It incorporates good features of other shells and a
19.I history
20mechanism similar to the
21.I redo
22of \s-2INTERLISP\s0.
23While incorporating many features of other shells which make
24writing shell programs (shell scripts) easier,
25most of the features unique to
26.I csh
27are designed more for the interactive \s-2UNIX\s0 user.
28.PP
29\s-2UNIX\s0
30users who have read a general introduction to the system
31will find a valuable basic explanation of the shell here.
32Simple terminal interaction with
33.I csh
34is possible after reading just the first section of this document.
35The second section describes the shells capabilities which you can
36explore after you have begun to become acquainted with the shell.
37Later sections introduce features which are useful, but not necessary
38for all users of the shell.
39.PP
40Back matter includes an appendix listing special characters of the shell
41and a glossary of terms and commands introduced in this manual.
42.AE
43.SH
44.if n .ND
45Introduction
46.PP
47A
48.I shell
49is a command language interpreter.
50.I Csh
51is the name of one particular command interpreter on
52\s-2UNIX\s0.
53The primary purpose of
54.I csh
55is to translate command lines typed at a terminal into
56system actions, such as invocation of other programs.
57.I Csh
58is a user program just like any you might write.
59Hopefully,
60.I csh
61will be a very useful program for you
62in interacting with the \s-2UNIX\s0 system.
63.PP
64In addition to this document, you will want to refer to a copy
65of the ``\s-2UNIX\s0 Programmers Manual.''
66The
67.I csh
68documentation in the manual provides a full description of all
69features of the shell and is a final reference for questions
70about the shell.
71.PP
72Many words in this document are shown in
73.I italics.
74These are important words;
75names of commands, and words which have special meaning in discussing
76the shell and \s-2UNIX\s0.
77Many of the words are defined in a glossary at the end of this document.
78If you don't know what is meant by a word, you should look
79for it in the glossary.
80.SH
81Acknowledgements
82.PP
83Numerous people have provided good input about previous versions
84of
85.I csh
86and aided in its debugging and in the debugging of its documentation.
87I would especially like to thank Michael Ubell
88who made the crucial observation that history commands could be
89done well over the word structure of input text, and implemented
90a prototype history mechanism in an older version of the shell.
91Eric Allman has also provided a large number of useful comments on the
92shell, helping to unify those concepts which are present and to identify
93and eliminate useless and marginally useful features.
94.br
95.bp
96.NH
97Terminal usage of the shell
98.NH 2
99The basic notion of commands
100.PP
101A
102.I shell
103in
104\s-2UNIX\s0
105acts mostly as a medium through which other
106.I commands
107are invoked.
108While it has a set of
109.I builtin
110commands which it performs directly,
111most useful commands are, in fact, external to the shell.
112The shell is thus distinguished from the command interpreters of other
113systems both by the fact that it is just a user program, and by the fact
114that it is used almost exclusively as a mechanism for invoking other programs.
115.PP
116Commands in the \s-2UNIX\s0 system expect a list of strings or
117.I words
118as arguments.
119Thus the command
120.DS
121mail bill
122.DE
123consists of two words.
124The first word
125.I mail
126names the command to be executed, in this case the
127mail program which sends messages to other users.
128The shell uses the name of the command in attempting to run it for you.
129It will look in a number of
130.I directories
131for a file with the name
132.I mail
133which is expected to contain the mail program.
134.PP
135The rest of the words of the command are given to the command itself to execute.
136In this case we specified also the word
137.I bill
138which is interpreted by the
139.I mail
140program to be the name of a user to whom mail is to be sent.
141In normal terminal usage we might use the
142.I mail
143command as follows.
144.DS
145% mail bill
146I have a question about the csh documentation.
147My document seems to be missing page 5.
148Does a page five exist?
149 Bill
150%
151.DE
152.PP
153Here we typed a message to send to
154.I bill
155and ended this message with a control-d which sent an end-of-file to
156the mail program.
157The mail program
158then transmitted our message.
159The characters `% ' were printed before and after the mail command
160by the shell to indicate that input was needed.
161.PP
162After typing the `% ' prompt the shell was reading command input from
163our terminal.
164We typed a complete command `mail bill'.
165The shell then executed the
166.I mail
167program with argument
168.I bill
169and went dormant waiting for it to complete.
170The mail program then read input from our terminal until we signalled
171an end-of-file after which the shell noticed that mail had completed
172and signaled us that it was ready to read from the terminal again by
173printing another `% ' prompt.
174.PP
175This is the essential pattern of all interaction with \s-2UNIX\s0
176through the shell.
177A complete command is typed at the terminal, the shell executes
178the command and when this execution completes prompts for a new command.
179If you run the editor for an hour, the shell will patiently wait for
180you to finish editing and obediently prompt you again whenever you finish
181editing.
182.NH 2
183Flag arguments
184.PP
185A useful notion in \s-2UNIX\s0 is that of a
186.I flag
187argument.
188While many arguments to commands specify file names or user names
189some arguments rather specify an optional capability of the command
190which you wish to invoke.
191By convention, such arguments begin with the character `\-'.
192Thus the command
193.DS
194ls
195.DE
196will produce a list of the files in the current directory.
197The option
198.I \-s
199is the size option, and
200.DS
201ls \-s
202.DE
203causes
204.I ls
205to also give, for each file the size of the file in blocks of 512
206characters.
207The manual page for each command in the \s-2UNIX\s0 programmers manual
208gives the available options for each command.
209The
210.I ls
211command has a large number of useful and interesting options.
212Most other commands have either no options or only one or two options.
213It is hard to remember options of commands which are not used very
214frequently, so most \s-2UNIX\s0 utilities perform only one or two functions
215rather than having a large number of hard to remember options.
216.NH 2
217Output to files
218.PP
219Many commands may read input or write output to files rather
220than simply taking input and output from the terminal.
221Each such command could take special words as arguments indicating
222where the output is to go.
223It is simpler, and usually
224sufficient, to connect these commands to files to which they wish to
225write, within the shell itself, and just before they are executed.
226.PP
227Thus suppose we wish to save the current date in a file called `now'.
228The command
229.DS
230date
231.DE
232will print the current date on our terminal.
233This is because our terminal is the default
234.I "standard output"
235for the date command and the date command prints the date on its
236standard output.
237The shell lets us redirect the
238.I "standard output"
239of a command through a
240notation using the
241.I metacharacter
242`>' and the name of the file where output is to be placed.
243Thus the command
244.DS
245date > now
246.DE
247runs the
248.I date
249command in an environment where its standard output is
250the file `now' rather than our terminal.
251Thus this command places the current date and time in the file `now'.
252It is important to know that the
253.I date
254command was unaware that its output was going to a file rather than
255to our terminal.
256The shell performed this
257.I redirection
258before the command began executing.
259.PP
260One other thing to note here is that the file `now'
261need not have existed before the
262.I date
263command was executed; the shell would have created the file if it did
264not exist.
265And if the file did exist?
266If it had existed previously these previous contents would have been discarded!
267A shell option
268.I noclobber
269exists to prevent this from happening accidentally;
270it is discussed in section 2.2.
271.NH 2
272Metacharacters in the shell
273.PP
274The shell has a large number of
275special characters (like `>')
276which indicate special functions.
277We say that these notations have
278.I syntactic
279and
280.I semantic
281meaning to the shell.
282In general, most characters which are neither letters nor digits
283have special meaning to the shell.
284We shall shortly learn a means of
285.I quotation
286which allows us to create words which contain
287.I metacharacters
288and to thus work without constantly worrying about whether certain
289characters are metacharacters.
290.PP
291Note that the shell is only reading input when it has prompted with
292`% '.
293Thus metacharacters will normally have effect only then.
294We need not worry about placing shell metacharacters in a letter
295we are sending via
296.I mail.
297.NH 2
298Input from files; pipelines
299.PP
300We learned above how to route the standard output of a command
301to a file.
302It is also possible to route the standard input of a command from a file.
303This is not often necessary since most commands will read from
304a file name given as argument.
305We can give the command
306.DS
307sort < data
308.DE
309to run the
310.I sort
311command with standard input, where the command normally
312reads, from the file
313`data'.
314We would more likely say
315.DS
316sort data
317.DE
318letting the
319.I sort
320command open the file
321`data'
322for input itself since this is less to type.
323.PP
324We should note that if we just typed
325.DS
326sort
327.DE
328then the sort program would sort lines from its
329.I "standard input."
330Since we did not
331.I redirect
332the standard input, it would sort lines as we typed them on the terminal
333until we typed a control-d to generate an end-of-file.
334.PP
335A most useful capability is the ability to combine the standard output
336of one command with the standard input of the next, i.e. to run the
337commands in a sequence known as a
338.I pipeline.
339For instance the command
340.DS
341ls \-s
342.DE
343normally produces a list of the files in our directory with the size
344of each in blocks of 512 characters.
345If we are interested in learning which of our files is largest we
346may wish to have this sorted by size rather than by name, which is
347the default way in which
348.I ls
349sorts.
350We could look at the many options of
351.I ls
352to see if there was an option to do this but would eventually discover
353that there is not.
354Instead we can use a couple of simple options of the
355.I sort
356command, combining it with
357.I ls
358to get what we want.
359.PP
360The
361.I \-n
362option of sort specifies a numeric sort rather than an alphabetic sort.
363Thus
364.DS
365ls \-s | sort \-n
366.DE
367specifies that the output of the
368.I ls
369command run with the option
370.I \-s
371is to be
372.I piped
373to the command
374.I sort
375run with the numeric sort option.
376This would give us a sorted list of our files by size, but with the
377smallest first.
378We could then use the
379.I \-r
380reverse sort option and the
381.I head
382command in combination with the previous command doing
383.DS
384ls \-s | sort \-n \-r | head \-5
385.DE
386Here we have taken a list of our files sorted alphabetically,
387each with the size in blocks.
388We have run this to the standard input of the
389.I sort
390command asking it to sort numerically in reverse order (largest first).
391This output has then been run into the command
392.I head
393which gives us the first few lines out.
394In this case we have asked
395.I head
396for the first 5 lines.
397Thus this command gives us the names and sizes of our 5 largest files.
398.PP
399The metanotation introduced above is called the
400.I pipe
401mechanism.
402Commands separated by `\||\|' characters are connected together by the
403shell and the output of each is run into the input of the next.
404The leftmost command in a pipeline will normally take its standard
405input from the terminal and the rightmost will place its standard
406output on the terminal.
407Other examples of pipelines will be given later when we discuss the
408history mechanism;
409one important use of pipes which is illustrated there is in the
410routing of information to the line printer.
411.NH 2
412Filenames
413.PP
414Many commands to be executed will need the names of files as arguments.
415\s-2UNIX\s0 pathnames consist of a number of components separated by `/'.
416Each component except the last names a directory in which the next
417component resides.
418Thus the pathname
419.DS
420/etc/motd
421.DE
422specifies a file in the directory
423`etc'
424which is a subdirectory of the
425.I root
426directory `/'.
427Within this directory the file named is `motd' which stands
428for `message of the day'.
429Filenames which do not begin with `/' are interpreted starting at
430the current
431.I working
432directory.
433This directory is, by default, your
434.I home
435directory and can be changed dynamically by the
436.I chdir
437change directory command.
438.PP
439Most filenames consist of a number of alphanumeric characters and `.'s.
440In fact, all printing characters except `/' may appear in filenames.
441It is inconvenient to have most non-alphabetic characters in filenames
442because many of these have special meaning to the shell.
443The character `.' is not a shell-metacharacter and is often used as
444the prefix with an
445.I extension
446of a base name.
447Thus
448.DS
449prog.c prog.o prog.errs prog.output
450.DE
451are four related files.
452They share a
453.I root
454portion of a name
455(a root portion being that part of the name that is left when a trailing
456`.' and following characters which are not `.' are stripped off).
457The file
458`prog.c'
459might be the source for a C program,
460the file `prog.o' the corresponding object file,
461the file
462`prog.errs' the errors resulting from a compilation of the program
463and the file
464`prog.output' the output of a run of the program.
465.PP
466If we wished to refer to all four of these files in a command, we could
467use the metanotation
468.DS
469prog.*
470.DE
471This word is expanded by the shell, before the command to which it is
472an argument is executed, into a list of names which begin with `prog.'.
473The character `*' here matches any sequence (including the empty sequence)
474of characters in a file name.
475The names which match are sorted into the argument list to the command
476alphabetically.
477Thus the command
478.DS
479echo prog.*
480.DE
481will echo the names
482.DS
483prog.c prog.errs prog.o prog.output
484.DE
485Note that the names are in lexicographic order here, and a different
486order than we listed them above.
487The
488.I echo
489command receives four words as arguments, even though we only typed
490one word as as argument directly.
491The four words were generated by filename expansion of the metasyntax
492in the one input word.
493.PP
494Other metanotations for
495.I "filename expansion"
496are also available.
497The character `?' matches any single character in a filename.
498Thus
499.DS
500echo ? \|?? \|???
501.DE
502will echo a line of filenames; first those with one character names,
503then those with two character names, and finally those with three
504character names.
505The names of each length will be independently lexicographically sorted.
506.PP
507Another mechanism consists of a sequence of characters between `[' and `]'.
508This metasequence matches any single character from the enclosed set.
509Thus
510.DS
511prog.[co]
512.DE
513will match
514.DS
515prog.c prog.o
516.DE
517in the example above.
518We can also place two characters astride a `\-' in this notation to denote
519a range.
520Thus
521.DS
522chap.[1\-5]
523.DE
524might match files
525.DS
526chap.1 chap.2 chap.3 chap.4 chap.5
527.DE
528if they existed.
529This is shorthand for
530.DS
531chap.[12345]
532.DE
533and otherwise equivalent.
534.PP
535An important point to note is that if a list of argument words to
536a command (an
537.I "argument list)"
538contains filename expansion syntax, and if this filename expansion syntax
539fails to match any existing file names, then the shell considers this
540to be an error and prints a diagnostic
541.DS
542No match.
543.DE
544.PP
545Another very important point is that the character `.' at the beginning
546of a filename is treated specially.
547Neither `*' or `?' or the `[' `]' mechanism will match it.
548This prevents accidental matching of the filenames `.' and `..'
549in the current directory which have special meaning to the system,
550as well as other files such as
551.I \&.cshrc
552which are not normally
553visible.
554We will discuss the special role of the file
555.I \&.cshrc
556later.
557.PP
558Another filename expansion mechanism gives access to the pathname of
559the
560.I home
561directory of other users.
562This notation consists of the character `~' followed by another users
563login name.
564For instance the word `~bill' would map to the pathname `/mnt/bill'
565if the home directory for `bill' was in the directory `/mnt/bill'.
566Since, on large systems, users may have login directories scattered over
567many different disk volumes with different prefix directory names,
568this notation provides a reliable way of accessing the files
569of other users.
570.PP
571A special case of this notation consists of a `~' alone, e.g. `~/mbox'.
572This notation is expanded by the shell into the file `mbox' in your
573.I home
574directory, i.e. into `/mnt/bill/mbox' for me on the Cory Hall \s-2UNIX\s0
575system.
576This can be very useful if you have used
577.I chdir
578to change to another users directory and have found a file you wish to
579copy using
580.I cp.
581You can do
582.DS
583cp thatfile ~
584.DE
585which will be expanded by the shell to
586.DS
587cp thatfile /mnt/bill
588.DE
589e.g., which the copy command will interpret as a request to make a copy of
590`thatfile'
591in the directory `/mnt/bill'.
592The `~' notation doesn't, by itself, force named files to exist.
593This is useful, for example, when using the
594.I cp
595command,
596e.g.
597.DS
598cp thatfile ~/saveit
599.DE
600.PP
601There also exists a mechanism using the characters `{' and `}' for
602abbreviating a set of word which have common parts but cannot
603be abbreviated by the above mechanisms because they are not files,
604are the names of files which do not yet exist,
605are not thus conveniently described.
606This mechanism will be described much later,
607in section 4.1,
608as it is used much less frequently.
609.NH 2
610Quotation
611.PP
612We have already seen a number of metacharacters used by the shell.
613These metacharacter pose a problem in that we cannot use them directly
614as parts of words.
615Thus the command
616.DS
617echo *
618.DE
619will not echo the character `*'.
620It will either echo an sorted list of filenames in the
621current directory, or print the message `No match' if there are
622no files in the current directory.
623.PP
624The recommended mechanism for placing characters which are neither numbers,
625digits, `/', `.' or `\-' in an argument word to a command is to enclose
626it with single quotation characters `\'', i.e.
627.DS
628echo \'*\'
629.DE
630There is one special character `!' which is used by the
631.I history
632mechanism of the shell and which cannot be
633.I escaped
634in this way.
635It and the character `\'' itself can be preceded by a single `\e'
636to prevent their special meaning.
637These two mechanisms suffice to place any printing character into a word
638which is an argument to a shell command.
639.NH 2
640Terminating commands
641.PP
642When you are running a command from the shell and the shell is
643dormant waiting for it to complete there are a couple of ways in
644which you can force such a command to complete.
645For instance if you type the command
646.DS
647cat /etc/passwd
648.DE
649the system will print a copy of a list of all users of the system
650on your terminal.
651This is likely to continue for several minutes unless you stop it.
652You can send an
653\s-2INTERRUPT\s0
654signal to the
655.I cat
656command by hitting the
657\s-2DEL\s0 or \s-2RUBOUT\s0
658key on your terminal.
659Actually, hitting this key sends this
660\s-2INTERRUPT\s0
661signal to all programs running on your terminal, including your shell.
662The shell normally ignores such signals however, so that the only
663program affected by the
664\s-2INTERRUPT\s0
665will be
666.I cat.
667Since
668.I cat
669does not take any precautions to catch this signal
670the
671\s-2INTERRUPT\s0
672will cause it to terminate.
673The shell notices that
674.I cat
675has died and prompts you again with `% '.
676If you hit \s-2INTERRUPT\s0 again, the shell will just
677repeat its prompt since it catches \s-2INTERRUPT\s0 signals
678and chooses to continue to execute commands rather than going away
679like
680.I cat
681did, which would have the effect of logging you out.
682.PP
683Another way in which many programs terminate is when they get an end-of-file
684from their standard input.
685Thus the
686.I mail
687program in the first example above was terminated when we hit a control-d
688which generates and end-of-file from the standard input.
689The shell also terminates when it gets an end-of-file printing `logout';
690\s-2UNIX\s0 then logs you off the system.
691Since this means that typing too many control-d's can accidentally log us off,
692the shell has a mechanism for preventing this.
693This
694.I ignoreeof
695option will be discussed in section 2.2.
696.PP
697If a command has its standard input redirected from a file, then it will
698normally terminate when it reaches the end of this file.
699Thus if we execute
700.DS
701mail bill < prepared.text
702.DE
703the mail command will terminate without our typing a control-d.
704This is because it read to the end-of-file of our file
705`prepared.text' in which we placed a message for `bill' with an editor.
706We could also have done
707.DS
708cat prepared.text \||\| mail bill
709.DE
710since the
711.I cat
712command would then have written the text through the pipe to the
713standard input of the mail command.
714When the
715.I cat
716command completed it would have terminated,
717closing down the pipeline
718and the
719.I mail
720command would have received an end-of-file from it and terminated.
721Using a pipe here is more complicated than redirecting input
722so we would more likely use the first form.
723These commands could also have been stopped by sending an \s-2INTERRUPT\s0.
724.PP
725If you write or run programs which are not fully debugged then it may
726be necessary to stop them somewhat ungracefully.
727This can be done by sending them a \s-2QUIT\s0
728signal, generated by a control-\e.
729This will usually provoke the shell to produce a message like:
730.DS
731a.out: Quit \-\- Core dumped
732.DE
733indicating that a file
734`core' has been created containing information about the program `a.out's
735state when it ran amuck.
736You can examine this file yourself, or forward information to the
737maintainer of the program telling him/her where the
738.I "core file"
739is.
740.PP
741If you run background commands (as explained in section 2.6) then these
742commands will ignore \s-2INTERRUPT\s0 and \s-2QUIT\s0 signals at the
743terminal. To stop them you must use the
744.I kill
745program. See section 2.6 for an example.
746.NH 2
747What now?
748.PP
749We have so far seen a number of mechanisms of the shell and learned a lot
750about the way in which it operates.
751The remaining sections will go yet further into the internals of the
752shell, but you will surely want to try using the shell before you go any further.
753To try it you can log in to \s-2UNIX\s0 and type the following
754command to the system:
755.DS
756chsh myname /bin/csh
757.DE
758Here
759`myname'
760should be replaced by the name you typed to
761the system prompt of
762`login:' to get onto the system.
763Thus I would use `chsh bill /bin/csh'.
764.B
765You only have to do this once; it takes effect at next login.
766.R
767You are now ready to try using
768.I csh.
769.PP
770Before you do the `chsh' command, the shell you are using when
771you log into the system is `/bin/sh'.
772In fact, much of the above discussion is applicable to `/bin/sh'.
773The next section will introduce many features particular to
774.I csh
775so you should change your shell to
776.I csh
777before you begin reading it.
778.bp
779.NH
780Details on the shell for terminal users
781.NH 2
782Shell startup and termination
783.PP
784When you login, the shell is placed by the system in your
785.I home
786directory and begins by reading commands from a file
787.I \&.cshrc
788in this directory.
789All shells which you may create during your terminal session will
790read from this file.
791We will later see what kinds of commands are usefully placed there.
792For now we need not have this file and the shell does not complain about
793its absence.
794.PP
795A
796.I login
797shell, executed after you login to the system,
798will, after it reads commands from
799.I \&.cshrc,
800read commands from a file
801.I \&.login
802also in your home directory.
803This file contains commands which you wish to do each time you login
804to the \s-2UNIX\s0 system.
805My
806.I \&.login
807file looks something like:
808.DS
809tset \-d adm3a \-p adm3a
810fixexrc
811set history=20
812set time=3
813.DE
814on the \s-2CORY\s0 Hall \s-2UNIX\s0 system.
815This file contains four commands to be executed by \s-2UNIX\s0
816each time I login.
817The first is a
818.I tset
819command which informs the system that I usually dial in on a Lear-Siegler
820\s-2ADM\-3A\s0 terminal and that if I am on a patchboard port on the
821fifth floor of Evans Hall I am probably also on an \s-2ADM\-3A\s0.
822The second command is a
823.I fixexrc
824which manipulates my
825.I ex
826startup file
827in certain ways if I am on a dialup port.
828We need not be concerned with exactly what this command does.
829In general you may have certain commands in your
830.I \&.login
831which are particular to you.
832.PP
833The next two
834.I set
835commands are interpreted directly by the shell and affect the values
836of certain shell variables to modify the future behavior of the shell.
837Setting the variable
838.I time
839tells the shell to print time statistics on commands which take more
840than a certain threshold of machine time (in this case 3 \s-2CPU\s0
841seconds).
842Setting the variable
843.I history
844tells the shell how much history of previous command words it should
845save in case I wish to repeat or rerun modified versions of previous
846commands.
847Since there is a certain overhead in this mechanism the shell does
848not set this variable by default, but rather lets users who wish to
849use the mechanism set it themselves.
850The value of 20 is a reasonably large value to assign to
851.I history.
852More casual users of the
853.I history
854mechanism
855would probably set a value of 5 or 10.
856The use of the
857.I history
858mechanism will be described subsequently.
859.PP
860After executing commands from
861.I \&.login
862the shell reads commands from your terminal, prompting for each with
863`% '.
864When it receives an end-of-file from the terminal, the shell
865will print `logout' and execute commands from the file `.logout'
866in your home directory.
867After that the shell will die and \s-2UNIX\s0 will log you off the system.
868If the system is not going down, you will receive a new login message.
869In any case, after the `logout' message the shell is doomed
870and will take no further input from the terminal.
871.NH 2
872Shell variables
873.PP
874The shell maintains a set of
875.I variables.
876We saw above the variables
877.I history
878and
879.I time
880which had values `20' and `3'.
881In fact, each shell variable has as value an array of
882zero or more
883.I strings.
884Shell variables may be assigned values by the set command. It has
885several forms, the most useful of which was given above and is
886.DS
887set name=value
888.DE
889.PP
890Shell variables may be used to store values which are to
891be reintroduced into commands later through a substitution mechanism.
892The shell variables most commonly referenced are, however, those which the
893shell itself refers to.
894By changing the values of these variables one can directly affect the
895behavior of the shell.
896.PP
897One of the most important variables is the variable
898.I path.
899This variable contains a sequence of directory names where the shell
900searches for commands.
901The
902.I set
903command
904shows the value of all variables currently defined (we usually say
905.I set)
906in the shell.
907The default value for path will be shown by
908.I set
909to be
910.DS
911% set
912argv
913home /mnt/bill
914path (. /bin /usr/bin)
915prompt %
916shell /bin/csh
917status 0
918%
919.DE
920This notation indicates that the variable path points to the current
921directory `.' and then `/bin' and `/usr/bin'.
922Commands which you may write might be in `.' (usually one of
923your directories).
924The most heavily used system commands live in `/bin'.
925Less heavily used system commands live in `/usr/bin'.
926.PP
927A number of new programs on the system live in the directory
928`/usr/new'.
929If we wish, as well we might, all shells which we invoke to have
930access to these new programs we can place the command
931.DS
932set path=(. /usr/new /bin /usr/bin)
933.DE
934in our file
935.I \&.cshrc
936in our home directory.
937Try doing this and then logging out and back in and do
938.DS
939set
940.DE
941again to see that the value assigned to
942.I path
943has changed.
944.PP
945Other useful built in variables are the variable
946.I home
947which shows your home directory,
948the variable
949.I ignoreeof
950which can be set in your
951.I \&.login
952file to tell the shell not to exit when it receives an end-of-file from
953a terminal.
954To logout from \s-2UNIX\s0
955with
956.I ignoreeof
957set you must type
958.DS
959logout
960.DE
961This is one of several variables which the shell does not care about the
962value of, only whether they are
963.I set
964or
965.I unset.
966Thus to set this variable you simply do
967.DS
968set ignoreeof
969.DE
970and to unset it do
971.DS
972unset ignoreeof
973.DE
974Both
975.I set
976and
977.I unset
978are built-in commands of the shell.
979.PP
980Finally, some other built-in shell variables of use are the
981variables
982.I noclobber
983and
984.I mail.
985The metasyntax
986.DS
987> filename
988.DE
989which redirects the output of a command
990will overwrite and destroy the previous contents of the named file.
991In this way you may accidentally overwrite a file which is valuable.
992If you would prefer that the shell not overwrite files in this
993way you can
994.DS
995set noclobber
996.DE
997in your
998.I \&.login
999file.
1000Then trying to do
1001.DS
1002date > now
1003.DE
1004would cause a diagnostic if `now' existed already.
1005You could type
1006.DS
1007date >! now
1008.DE
1009if you really wanted to overwrite the contents of `now'.
1010The `>!' is a special metasyntax indicating that clobbering the
1011file is ok.
1012.PP
1013If you receive mail frequently while you are logged in and wish to
1014be informed of the arrival of this mail you can put a command
1015.DS
1016set mail=/usr/mail/yourname
1017.DE
1018in your
1019.I \&.login
1020file.
1021Here you should change `yourname' to your login name.
1022The shell will look at this file every 10 minutes to see if new mail has arrived.
1023If you receive mail only infrequently you are better off not
1024setting this variable. In this case it will only serve to delay the shells
1025response to you when it checks for mail.
1026.PP
1027The use of shell variables to introduce text into commands, which is
1028most useful in shell command scripts, will be introduced in section 2.4.
1029.NH 2
1030The shell's history list
1031.PP
1032The shell can maintain a history list into which it places the words
1033of previous commands.
1034It is possible to use a metanotation to reintroduce commands or words
1035from commands in forming new commands.
1036This mechanism can be used to repeat previous commands or to
1037correct minor typing mistakes in commands.
1038.PP
1039Consider the following transcript:
1040.DS
1041% where michael
1042michael is on tty0 dialup 300 baud 642-7927
1043% write !$
1044write michael
1045Long time no see michael.
1046Why don't you call me at 524-4510.
1047EOF
1048%
1049.DE
1050Here we asked the system where
1051.I michael
1052was logged in.
1053It told us he was on `tty0'
1054and we told the shell to invoke a `write' command to
1055`!$'.
1056This is a history notation which means the last word of the last
1057command executed, in this case `michael'.
1058The shell performed this substitution and then echoed the command
1059as it would execute it.
1060Let us assume that we don't hear anything from michael.
1061We might do
1062.DS
1063% ps t0
1064 PID TTY TIME COMMAND
1065 4808 0 0:05 \-
1066% !!
1067ps t0
1068 PID TTY TIME COMMAND
1069 5104 0 0:00 \- 7
1070% !where
1071where michael
1072michael is not logged in
1073%
1074.DE
1075Here we ran a
1076.I ps
1077on the teletype
1078.I michael
1079was logged in on to see that he had a shell.
1080Repeating this command via the history substitution `!!'
1081we saw that he had logged out and that only a
1082.I getty
1083process was running on his terminal.
1084Repeating the
1085.I where
1086command
1087showed that he was indeed gone, most likely having
1088hung up the phone in order to be able to call.
1089.PP
1090This illustrates several useful features of the history mechanism.
1091The form `!!' repeats the last command execution.
1092The form `!string' repeats the last command which began with
1093a word of which `string' is a prefix.
1094Another useful command form is `\(ualhs\(uarhs' performing a substitute
1095similar to that in
1096.I ed
1097or
1098.I ex.
1099Thus after
1100.DS
1101% cat ~bill/csh/sh..c
1102/mnt/bill/csh/sh..c: No such file or directory
1103% \(ua..(ua.
1104cat ~bill/csh/sh.c
1105#include "sh.h"
1106
1107/*
1108 * C Shell
1109 *
1110 * Bill Joy, UC Berkeley
1111 * October, 1978
1112 */
1113
1114char *pathlist[] = { SRCHP
1115%
1116.DE
1117here we used the substitution to correct a typing mistake,
1118and then rubbed the command out after we saw that we had found the
1119file that we wanted.
1120The substitution changed the two `.' characters to a single `.'
1121character.
1122.PP
1123After this command we might do
1124.DS
1125% !! \||\| lpr
1126cat ~bill/csh/sh.c \||\| lpr
1127.DE
1128to put a copy of this file on the line printer, or (immediately
1129after the
1130.I cat
1131which worked above)
1132.DS
1133% pr !$ \||\| lpr
1134pr ~bill/csh/sh.c \||\| lpr
1135%
1136.DE
1137to print a copy on the printer using
1138.I pr.
1139.PP
1140More advanced forms of the history mechanism are also possible.
1141A notion of modification on substitutions allows one to say
1142(after the first successful
1143.I cat
1144above).
1145.DS
1146% cd !$:h
1147cd ~bill/csh
1148%
1149.DE
1150The trailing `:h' on the history substitution here causes only
1151the head portion of the pathname reintroduced by the history
1152mechanism to be substituted.
1153This mechanism and related mechanisms are used less often than
1154the forms above.
1155.PP
1156A complete description of history mechanism
1157features is given in the C shell manual pages in the \s-2UNIX\s0 Programmers Manual.
1158.NH 2
1159Aliases
1160.PP
1161The shell has an
1162.I alias
1163mechanism which can be used to make transformations on input commands.
1164This mechanism can be used to simplify the commands you type,
1165to supply default arguments to commands,
1166or to perform transformations on commands and their arguments.
1167The alias facility is similar to the macro facility of many assemblers.
1168.PP
1169Some of the features obtained by aliasing can be obtained also
1170using shell command files, but these take place in another instance
1171of the shell and cannot directly affect the current shells environment
1172and commands such as
1173.I chdir
1174which must be done in the current shell.
1175.PP
1176As an example, suppose that there is a new version of the mail program
1177on the system called `Mail'
1178you wish to use, rather than the standard mail program which is called `mail'.
1179If you place the shell command
1180.DS
1181alias mail Mail
1182.DE
1183in your
1184.I \&.login
1185file, the shell will transform an input line of the form
1186.DS
1187mail bill
1188.DE
1189into a call on `Mail'.
1190More generally, suppose we wish the command `ls' to always show
1191sizes of files, that is to always do `\-s'.
1192We can do
1193.DS
1194alias ls ls \-s
1195.DE
1196or even
1197.DS
1198alias dir ls \-s
1199.DE
1200creating a new command syntax `dir'
1201which does an `ls \-s'.
1202If we say
1203.DS
1204dir ~bill
1205.DE
1206then the shell will translate this to
1207.DS
1208ls \-s /mnt/bill
1209.DE
1210.PP
1211Thus the
1212.I alias
1213mechanism can be used to provide short names for commands,
1214to provide default arguments,
1215and to define new short commands in terms of other commands.
1216It is also possible to define aliases which contain multiple
1217commands or pipelines, showing where the arguments to the original
1218command are to be substituted using the facilities of the
1219history mechanism.
1220Thus the definition
1221.DS
1222alias cd \'cd \e!* ; ls \'
1223.DE
1224would do an
1225.I ls
1226command after each change directory
1227.I cd
1228command.
1229We enclosed the entire alias definition in `\'' characters to prevent
1230most substitutions from occuring and the character `;' from being
1231recognized as a parser metacharacter.
1232The `!' here is escaped with a `\e' to prevent it from being interpreted
1233when the alias command is typed in.
1234The `\e!*' here substitutes the entire argument list to the pre-aliasing
1235.I cd
1236command, without giving an error if there were no arguments.
1237The `;' separating commands is used here
1238to indicate that one command is to be done and then the next.
1239Similarly the definition
1240.DS
1241alias whois \'grep \e!\(ua /etc/passwd\'
1242.DE
1243defines a command which looks up its first argument in the password file.
1244.NH 2
1245Detached commands; >> and >& redirection
1246.PP
1247There are a few more metanotations useful to the terminal user
1248which have not been introduced yet.
1249The metacharacter `&' may be placed after a command, or after a sequence
1250of commands separated by `;' or `|'.
1251This causes the shell to not wait for the commands to terminate before
1252prompting again.
1253We say that they are
1254.I detached
1255or
1256.I background
1257processes. Thus
1258.DS
1259% pr ~bill/csh/sh.c \||\| lpr &
12605120
12615121
1262%
1263.DE
1264Here the shell printed two numbers and came back very quickly rather
1265than waiting for the
1266.I pr
1267and
1268.I lpr
1269commands to finish.
1270These numbers are the process numbers assigned by the system to the
1271.I pr
1272and
1273.I lpr
1274commands.\(dg
1275.FS
1276\(dgRunning commands in the background like this tends to slow down
1277the system and is not a good idea if the system is overloaded.
1278When overloaded, the system will just bog down more if you run a large
1279number of processes at once.
1280.FE
1281.PP
1282Since havoc would result if a command run in the background were to
1283read from your terminal at the same time as the shell does, the
1284default standard input for a command run in the background is
1285not your terminal, but an empty file called `/dev/null'.
1286Commands run in the background are also made immune to
1287\s-2INTERRUPT\s0 and \s-2QUIT\s0 signals which you may subsequently
1288generate at your terminal.*
1289.FS
1290*If a background command stops suddenly when you hit \s-2INTERRUPT\s0
1291or \s-2QUIT\s0 it is likely a bug in the background program.
1292.FE
1293.PP
1294If you intend to log off the system before the command completes you
1295must run the command immune to
1296\s-2HANGUP\s0
1297signals.
1298This is done by placing the word
1299`nohup' before each program in the command, i.e.:
1300.DS
1301nohup man csh \||\| nohup lpr &
1302.DE
1303.PP
1304In addition to the standard output, commands also have a
1305diagnostic output
1306which is normally directed to the terminal even when the standard output
1307is directed to a file or a pipe.
1308It is occasionally desirable to direct the diagnostic output along with
1309the standard output.
1310For instance if you want to redirect the output of a long running command
1311into a file and wish to have a record of any error diagnostic it produces
1312you can do
1313.DS
1314command >& file
1315.DE
1316The `>&' here tells the shell to route both the diagnostic output and the
1317standard output into `file'.
1318of the standard output.
1319Similarly you can give the command
1320.DS
1321command |\|& lpr
1322.DE
1323to route both standard and diagnostic output through the pipe
1324to the line printer daemon
1325.I lpr.#
1326.FS
1327#A command form
1328.DS
1329command >&! file
1330.DE
1331exists, and is used when
1332.I noclobber
1333is set and
1334.I file
1335already exists.
1336.FE
1337.PP
1338Finally, it is possible to use the form
1339.DS
1340command >> file
1341.DE
1342to place output at the end of an existing file.\(dg
1343.FS
1344\(dgIf
1345.I noclobber
1346is set, then an error will result if
1347.I file
1348does not exist, otherwise the shell will create
1349.I file
1350if it doesn't exist.
1351A form
1352.DS
1353command >>! file
1354.DE
1355makes it not be an error for file to not exist when
1356.I noclobber
1357is set.
1358.FE
1359.NH 2
1360Useful built-in commands
1361.PP
1362We now give a few of the useful built-in commands of the shell describing
1363how they are used.
1364.PP
1365The
1366.I alias
1367command described above is used to assign new aliases and to show the
1368existing aliases.
1369With no arguments it prints the current aliases.
1370It may also be given an argument such as
1371.DS
1372alias ls
1373.DE
1374to show the current alias for, e.g., `ls'.
1375.PP
1376The
1377.I cd
1378and
1379.I chdir
1380commands are equivalent, and change the working directory of the shell.
1381It is useful to make a directory for each project you wish to work on
1382and to place all files related to that project in that directory.
1383Thus after you login you can do
1384.DS
1385% pwd
1386/mnt/bill
1387% mkdir newpaper
1388% chdir newpaper
1389% pwd
1390/mnt/bill/newpaper
1391%
1392.DE
1393after which you will be in the directory
1394.I newpaper.
1395You can place a group of related files there.
1396You can return to your `home' login directory by doing just
1397.DS
1398chdir
1399.DE
1400with no arguments.
1401We used the
1402.I pwd
1403print working directory command to show the name of the current
1404directory here.
1405The current directory will usually be a subdirectory of your
1406home directory, and have it (here `/mnt/bill') at the start of it.
1407.PP
1408The
1409.I echo
1410command prints its arguments.
1411It is often used in shell scripts or as an interactive command
1412to see what filename expansions will yield.
1413.PP
1414The
1415.I history
1416command will show the contents of the history list.
1417The numbers given with the history events can be used to reference
1418previous events which are difficult to reference using the
1419contextual mechanisms introduced above.
1420There is also a shell variable called
1421.I prompt.
1422By placing a `!' character in its value the shell will there substitute
1423the index of the current command in the history list.
1424You can use this number to refer to this command in a history substitution.
1425Thus you could
1426.DS
1427set prompt=\'\e! % \'
1428.DE
1429Note that the `!' character had to be escaped here even within `\'' characters.
1430.PP
1431The
1432.I logout
1433command can be used to terminate a login shell which has
1434.I ignoreeof
1435set.
1436.PP
1437The
1438.I repeat
1439command can be used to repeat a command several times.
1440Thus to make 5 copies of the file
1441.I one
1442in the file
1443.I five
1444you could do
1445.DS
1446repeat 5 cat one >> five
1447.DE
1448.PP
1449The
1450.I setenv
1451command can be used, on version 7 \s-2UNIX\s0 systems,
1452to set variables in the environment.
1453Thus
1454.DS
1455setenv TERM adm3a
1456.DE
1457will set the value of the environment variable \s-2TERM\s0
1458to
1459`adm3a'.
1460A user program
1461.I printenv
1462exists which will print out the environment.
1463It might then show:
1464.DS
1465% printenv
1466HOME /usr/bill
1467SHELL /bin/csh
1468TERM adm3a
1469%
1470.DE
1471.PP
1472The
1473.I source
1474command can be used to force the current shell to read commands from
1475a file.
1476Thus
1477.DS
1478source .cshrc
1479.DE
1480can be used after editing in a change to the
1481.I \&.cshrc
1482file which you wish to take effect before the next time you login.
1483.PP
1484The
1485.I time
1486command can be used to cause a command to be timed no matter how much
1487\s-2CPU\s0 time it takes.
1488Thus
1489.DS
1490% time cp five five.save
14910.0u 0.3s 0:01 26%
1492% time wc five.save
1493 1200 6300 37650 five.save
14941.2u 0.5s 0:03 55%
1495%
1496.DE
1497indicates that the
1498.I cp
1499command used less that 1/10th of a second of user time and only
15003/10th of a second of system time in copying the file `five' to
1501`five.save'.
1502The command word count
1503`wc'
1504on the other hand used 1.2 seconds of user time and 0.5 seconds of system
1505time in 3 seconds of elapsed time in counting the number of words,
1506character and lines in `five.save'.
1507The percentage `55%' indicates that over this period of 3 seconds,
1508our command `wc' used an average of 55 percent of the available \s-2CPU\s0
1509cycles of the machine.
1510This is a very high percentage and indicates that the system is lightly
1511loaded.
1512.PP
1513The
1514.I unalias
1515and
1516.I unset
1517commands can be used
1518to remove aliases and variable definitions from the shell.
1519.PP
1520The
1521.I wait
1522command can be used after starting processes with `&'
1523to quickly see if they have finished.
1524If the shell responds immediately with another prompt, they have.
1525Otherwise you can wait for the shell to prompt at which point they
1526will have finished, or interrupt the shell by sending a \s-2RUB\s0
1527or \s-2DELETE\s0 character.
1528If the shell is interrupted, it will print the names and numbers of the
1529processes it knows to be unfinished.
1530Thus:
1531.DS
1532% nroff paper \||\| lpr &
15332450
15342451
1535% wait
1536 2451 lpr
1537 2450 nroff
1538wait: Interrupted.
1539%
1540.DE
1541.PP
1542You can check again later by doing another
1543.I wait,
1544or see which commands are still running by doing a
1545.I ps.
1546As `time'
1547will show you,
1548.I ps
1549is fairly expensive.
1550It is thus counterproductive to run many
1551.I ps
1552commands to see how a background process is doing.\(dg
1553.FS
1554\(dgIf you do you are usurping with these
1555.I ps
1556commands the processor time the job needs to finish, thereby delaying its
1557completion!
1558.FE
1559.PP
1560If you run a background process and decide you want to stop it for whatever
1561reason you must use the
1562.I kill
1563program.
1564You must use the number of the processes you wish to kill.
1565Thus to stop the
1566.I nroff
1567in the above pipeline you would do
1568.DS
1569% kill 2450
1570% wait
15712450: nroff: Terminated.
1572%
1573.DE
1574Here the shell printed a diagnostic that we terminated `nroff' only
1575after we did a
1576.I wait.
1577If we want the shell to discover the termination of all processes
1578it has created we must, in general, use
1579.I wait.
1580.NH 2
1581What else?
1582.PP
1583This concludes the basic discussion of the shell for terminal users.
1584There are more features of the shell to be discussed here, and all
1585features of the shell are discussed in its manual pages.
1586One useful feature which is discussed later is the
1587.I foreach
1588built-in command which can be used to run the same command
1589sequence with a number of different arguments.
1590.PP
1591If you intend to use \s-2UNIX\s0 a lot you you should look through
1592the rest of this document and the shell manual pages to become familiar
1593with the other facilities which are available to you.
1594.bp
1595.NH
1596Shell control structures and command scripts
1597.NH 2
1598Introduction
1599.PP
1600It is possible to place commands in files and to cause shells to be
1601invoked to read and execute commands from these files,
1602which are called
1603.I "shell scripts."
1604We here detail those features of the shell useful to the writers of such
1605scripts.
1606.NH 2
1607Make
1608.PP
1609It is important to first note what shell scripts are
1610.I not
1611useful for.
1612There is a program called
1613.I make
1614which is very useful for maintaining a group of related files
1615or performing sets of operations on related files.
1616For instance a large program consisting of one or more files
1617can have its dependencies described in a
1618.I makefile
1619which contains definitions of the commands used to create these
1620different files when changes occur.
1621Definitions of the means for printing listings, cleaning up the directory
1622in which the files reside, and installing the resultant programs
1623are easily, and most appropriately placed in this
1624.I makefile.
1625This format is superior and preferable to maintaining a group of shell
1626procedures to maintain these files.
1627.PP
1628Similarly when working on a document a
1629.I makefile
1630may be created which defines how different versions of the document
1631are to be created and which options of
1632.I nroff
1633or
1634.I troff
1635are appropriate.
1636.NH 2
1637Invocation and the argv variable
1638.PP
1639A
1640.I csh
1641command script may be interpreted by saying
1642.DS
1643% csh script ...
1644.DE
1645where
1646.I script
1647is the name of the file containing a group of
1648.I csh
1649commands and
1650`\&...' is replaced by a sequence of arguments.
1651The shell places these arguments in the variable
1652.I argv
1653and then begins to read commands from the script.
1654These parameters are then available through the same mechanisms
1655which are used to reference any other shell variables.
1656.PP
1657If you make the file
1658`script'
1659executable by doing
1660.DS
1661chmod 755 script
1662.DE
1663and place a shell comment at the beginning of the shell script
1664(i.e. begin the file with a `#' character)
1665then a `/bin/csh' will automatically be invoked to execute `script' when
1666you type
1667.DS
1668script
1669.DE
1670If the file does not begin with a `#' then the standard shell
1671`/bin/sh' will be used to execute it.
1672This allows you to convert your older shell scripts to use
1673.I csh
1674at your convenience.
1675.NH 2
1676Variable substitution
1677.PP
1678After each input line is broken into words and history substitutions
1679are done on it, the input line is parsed into distinct commands.
1680Before each command is executed a mechanism know as
1681.I "variable substitution"
1682is done on these words.
1683Keyed by the character `$' this substitution replaces the names
1684of variables by their values.
1685Thus
1686.DS
1687echo $argv
1688.DE
1689when placed in a command script would cause the current value of the
1690variable
1691.I argv
1692to be echoed to the output of the shell script.
1693It is an error for
1694.I argv
1695to be unset at this point.
1696.PP
1697A number of notations are provided for accessing components and attributes
1698of variables.
1699The notation
1700.DS
1701$?name
1702.DE
1703expands to `1' if name is
1704.I set
1705or to `0'
1706if name is not
1707.I set.
1708It is the fundamental mechanism used for checking whether particular
1709variables have been assigned values.
1710All other forms of reference to undefined variables cause errors.
1711.PP
1712The notation
1713.DS
1714$#name
1715.DE
1716expands to the number of elements in the variable
1717.I name.
1718Thus
1719.DS
1720% set argv=(a b c)
1721% echo $?argv
17221
1723% echo $#argv
17243
1725% unset argv
1726% echo $?argv
17270
1728% echo $argv
1729Undefined variable: argv.
1730%
1731.DE
1732.PP
1733It is also possible to access the components of a variable
1734which has several values.
1735Thus
1736.DS
1737$argv[1]
1738.DE
1739gives the first component of
1740.I argv
1741or in the example above `a'.
1742Similarly
1743.DS
1744$argv[$#argv]
1745.DE
1746would give `c',
1747and
1748.DS
1749$argv[1\-2]
1750.DE
1751Other notations useful in shell scripts are
1752.DS
1753$\fIn\fR
1754.DE
1755where
1756.I n
1757is an integer as a shorthand for
1758.DS
1759$argv[\fIn\fR\|]
1760.DE
1761the
1762.I n\|th
1763parameter and
1764.DS
1765$*
1766.DE
1767which is a shorthand for
1768.DS
1769$argv
1770.DE
1771The form
1772.DS
1773$$
1774.DE
1775expands to the process number of the current shell.
1776Since this process number is unique in the system it can
1777be used in generation of unique temporary file names.
1778.PP
1779One minor difference between `$\fIn\fR\|' and `$argv[\fIn\fR\|]'
1780should be noted here.
1781The form
1782`$argv[\fIn\fR\|]'
1783will yield an error if
1784.I n
1785is not in the range
1786`1\-$#argv'
1787while `$n'
1788will never yield an out of range subscript error.
1789This is for compatibility with the way older shells handled parameters.
1790.PP
1791Another important point is that it is never an error to give a subrange
1792of the form `n\-'; if there are less than
1793.I n
1794components of the given variable then no words are substituted.
1795A range of the form `m\-n' likewise returns an empty vector without giving
1796an error when \fIm\fR exceeds the number of elements of the given variable,
1797provided the subscript \fIn\fR is in range.
1798.NH 2
1799Expressions
1800.PP
1801In order for interesting shell scripts to be constructed it
1802must be possible to evaluate expressions in the shell based on the
1803values of variables.
1804In fact, all the arithmetic operations of the language C are available
1805in the shell
1806with the same precedence that they have in C.
1807In particular, the operations `==' and `!=' compare strings
1808and the operators `&&' and `|\|\||' implement the boolean and/or operations.
1809.PP
1810The shell also allows file enquiries of the form
1811.DS
1812\-? filename
1813.DE
1814where `?' is replace by a number of single characters.
1815For instance the expression primitive
1816.DS
1817\-e filename
1818.DE
1819tell whether the file
1820`filename'
1821exists.
1822Other primitives test for read, write and execute access to the file,
1823whether it is a directory, or has non-zero length.
1824.PP
1825It is possible to test whether a command terminates normally,
1826by a primitive of the
1827form `{ command }' which returns true, i.e. `1' if the command
1828succeeds exiting normally with exit status 0, or `0' if the command
1829terminates abnormally or with exit status non-zero.
1830If more detailed information about the execution status of a command
1831is required, it can be executed and the variable `$status' examined
1832in the next command.
1833Since `$status' is set by every command, it is very transient.
1834It can be saved if it is inconvenient to use it only in the single
1835immediately following command.
1836.PP
1837For a full list of expression components available see the manual
1838section for the shell.
1839.NH 2
1840Sample shell script
1841.PP
1842A sample shell script which makes use of the expression mechanism
1843of the shell and some of its control structure follows:
1844.DS
1845% cat copyc
1846#
1847# Copyc copies those C programs in the specified list
1848# to the directory ~/backup if they differ from the files
1849# already in ~/backup
1850#
1851set noglob
1852foreach i ($argv)
1853
1854 if ($i:r.c != $i) continue # not a .c file so do nothing
1855
1856 if (! \-r ~/backup/$i:t) then
1857 echo $i:t not in backup... not cp\e\'ed
1858 continue
1859 endif
1860
1861 cmp \-s $i ~/backup/$i:t # to set $status
1862
1863 if ($status != 0) then
1864 echo new backup of $i
1865 cp $i ~/backup/$i:t
1866 endif
1867end
1868.DE
1869.PP
1870This script makes use of the
1871.I foreach
1872command, which causes the shell to execute the commands between the
1873.I foreach
1874and the matching
1875.I end
1876for each of the values given between `(' and `)' with the named
1877variable, in this case `i' set to successive values in the list.
1878Within this loop we may use the command
1879.I break
1880to stop executing the loop
1881and
1882.I continue
1883to prematurely terminate one iteration
1884and begin the next.
1885After the
1886.I foreach
1887loop the iteration variable
1888(\fIi\fR in this case)
1889has the value at the last iteration.
1890.PP
1891We set the variable
1892.I noglob
1893here to prevent filename expansion of the members of
1894.I argv.
1895This is a good idea, in general, if the arguments to a shell script
1896are filenames which have already been expanded or if the arguments
1897may contain filename expansion metacharacters.
1898It is also possible to quote each use of a `$' variable expansion,
1899but this is harder and less reliable.
1900.PP
1901The other control construct used here is a statement of the form
1902.DS
1903\fBif\fR ( expression ) \fBthen\fR
1904 command
1905 ...
1906\fBendif\fR
1907.DE
1908The placement of the keywords here is
1909.B not
1910flexible due to the current implementation of the shell.\(dg
1911.FS
1912\(dgThe following two formats are not currently acceptable to the shell:
1913.DS
1914\fBif\fR ( expression ) # \fBWon't work!\fR
1915\fBthen\fR
1916 command
1917 ...
1918\fBendif\fR
1919.DE
1920and
1921.DS
1922\fBif\fR ( expression ) \fBthen\fR command \fBendif\fR # \fBWon't work\fR
1923.DE
1924.FE
1925.PP
1926The shell does have another form of the if statement of the form
1927.DS
1928\fBif\fR ( expression ) \fBcommand\fR
1929.DE
1930which can be written
1931.DS
1932\fBif\fR ( expression ) \e
1933 command
1934.DE
1935Here we have escaped the newline for the sake of appearance,
1936and the `\e' must \fBimmediately\fR.
1937The command must not involve `\||\|', `&' or `;'
1938and must not be another control command.
1939The second form requires the final `\e' to
1940.B immediately
1941precede the end-of-line.
1942.PP
1943The more general
1944.I if
1945statements above also admit a sequence of
1946.I else\-if
1947pairs followed by a single
1948.I else
1949and an
1950.I endif,
1951e.g.:
1952.DS
1953\fBif\fR ( expression ) \fBthen\fR
1954 commands
1955\fBelse\fR \fBif\fR (expression ) \fBthen\fR
1956 commands
1957\&...
1958
1959\fBelse\fR
1960 commands
1961\fBendif\fR
1962.DE
1963.PP
1964Another important mechanism used in shell scripts is `:' modifiers.
1965We can use the modifier `:r' here to extract a root of a filename.
1966Thus if the variable
1967.I i
1968has the value
1969`foo.bar'
1970then
1971.DS
1972% echo $i $i:r
1973foo.bar foo
1974%
1975.DE
1976shows how the `:r' modifier strips off the trailing `.bar'.
1977Other modifiers will take off the last component of a pathname leaving
1978the head `:h' or all but the last component of a pathname leaving the
1979tail `:t'.
1980These modifiers are fully described in the
1981.I csh
1982manual pages in the programmers manual.
1983It is also possible to use the
1984.I "command substitution"
1985mechanism described in the next major section to perform modifications
1986on strings to then reenter the shells environment.
1987Since each usage of this mechanism involves the creation of a new process,
1988it is much more expensive to use than the `:' modification mechanism.#
1989.FS
1990#It is also important to note that
1991the current implementation of the shell limits the number of `:' modifiers
1992on a `$' substitution to 1.
1993Thus
1994.DS
1995% echo $i $i:h:t
1996/a/b/c /a/b:t
1997%
1998.DE
1999does not do what one would expect.
2000.FE
2001Finally, we note that the character `#' lexically introduces a shell
2002comment in shell scripts (but not from the terminal).
2003All subsequent characters on the input line after a `#' are discarded
2004by the shell.
2005This character can be quoted using `\'' or `\e' to place it in
2006an argument word.
2007.NH 2
2008Other control structures
2009.PP
2010The shell also has control structures
2011.I while
2012and
2013.I switch
2014similar to those of C.
2015These take the forms
2016.DS
2017\fBwhile\fR ( expression )
2018 commands
2019\fBend\fR
2020.DE
2021and
2022.DS
2023\fBswitch\fR ( word )
2024
2025\fBcase\fR str1:
2026 commands
2027 \fBbreaksw\fR
2028
2029\& ...
2030
2031\fBcase\fR strn:
2032 commands
2033 \fBbreaksw\fR
2034
2035\fBdefault:\fR
2036 commands
2037 \fBbreaksw\fR
2038
2039\fBendsw\fR
2040.DE
2041For details see the manual section for
2042.I csh.
2043C programmers should note that we use
2044.I breaksw
2045to exit from a
2046.I switch
2047while
2048.I break
2049exits a
2050.I while
2051or
2052.I foreach
2053loop.
2054A common mistake to make in
2055.I csh
2056scripts is to use
2057.I break
2058rather than
2059.I breaksw
2060in switches.
2061.PP
2062Finally,
2063.I csh
2064allows a
2065.I goto
2066statement, with labels looking like they do in C, i.e.:
2067.DS
2068loop:
2069 commands
2070 \fBgoto\fR loop
2071.DE
2072.NH 2
2073Supplying input to commands
2074.PP
2075Commands run from shell scripts receive by default the standard
2076input of the shell which is running the script.
2077This it is different from previous shells running
2078under \s-2UNIX\s0. It allowing shell scripts to fully participate
2079in pipelines, but mandates extra notation for commands which are to take
2080inline data.
2081.PP
2082Thus we need a metanotation for supplying inline data to commands in
2083shell scripts.
2084As an example, consider this script which runs the editor to
2085delete leading blanks from the lines in each argument file
2086.DS
2087% cat deblank
2088# deblank \-\- remove leading blanks
2089foreach i ($argv)
2090ed \- $i << \'EOF\'
20911,$s/\(ua[ ]*//
2092w
2093q
2094\&\'EOF\'
2095end
2096%
2097.DE
2098The notation `<< \'EOF\''
2099means that the standard input for the
2100.I ed
2101command is to come from the text in the shell script file
2102up to the next line consisting of exactly `\'EOF\''.
2103The fact that the `EOF' is enclosed in `\'' characters, i.e. quoted,
2104causes the shell to not perform variable substitution on the
2105intervening lines.
2106In general, if any part of the word following the `<<' which the
2107shell uses to terminate the text to be given to the command is quoted
2108then these substitutions will not be performed.
2109In this case since we used the form `1,$' in our editor script
2110we needed to insure that this `$' was not variable substituted.
2111We could also have insured this by preceding the `$' here with a `\e',
2112i.e.:
2113.DS
21141,\e$s/\(ua[ ]*//
2115.DE
2116but quoting the `EOF' terminator is a more reliable way of achieving the
2117same thing.
2118.NH 2
2119Catching interrupts
2120.PP
2121If our shell script creates temporary files, we may wish to catch
2122interruptions of the shell script so that we can clean up
2123these files.
2124We can then do
2125.DS
2126onintr label
2127.DE
2128where
2129.I label
2130is a label in our program.
2131If an interrupt is received the shell will do a
2132`goto label'
2133and we can remove the temporary files and then do a
2134.I exit
2135command (which is built in to the shell)
2136to exit from the shell script.
2137If we wish to exit with a non-zero status we can do
2138.DS
2139exit(1)
2140.DE
2141e.g. to exit with status `1'.
2142.NH 2
2143What else?
2144.PP
2145There are other features of the shell useful to writers of shell
2146procedures.
2147The
2148.I verbose
2149and
2150.I echo
2151options and the related
2152.I \-v
2153and
2154.I \-x
2155command line options can be used to help trace the actions of the shell.
2156The
2157.I \-n
2158option causes the shell only to read commands and not to execute
2159them and may sometimes be of use.
2160.PP
2161One other thing to note is that
2162.I csh
2163will not execute shell scripts which do not begin with the
2164character `#', that is shell scripts that do not begin with a comment.
2165Similarly, the `/bin/sh' on your system may well defer to `csh'
2166to interpret shell scripts which begin with `#'.
2167This allows shell scripts for both shells to live in harmony.
2168.PP
2169There is also another quotation mechanism using `"' which allows
2170only some of the expansion mechanisms we have so far discussed to occur
2171on the quoted string and serves to make this string into a single word
2172as `\'' does.
2173.bp
2174.NH 1
2175Miscellaneous, less generally useful, shell mechanisms
2176.NH 2
2177Loops at the terminal; variables as vectors
2178.PP
2179It is occasionally useful to use the
2180.I foreach
2181control structure at the terminal to aid in performing a number
2182of similar commands.
2183For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0
2184system at Cory Hall,
2185`/bin/sh',
2186`/bin/nsh',
2187and
2188`/bin/csh'.
2189To count the number of persons using each shell one could issue the commands
2190.DS
2191% grep \-c csh$ /etc/passwd
219227
2193% grep \-c nsh$ /etc/passwd
2194128
2195% grep \-c \-v sh$ /etc/passwd
2196430
2197%
2198.DE
2199Since these commands are very similar we can use
2200.I foreach
2201to do this more easily.
2202.DS
2203% foreach i (\'sh$\' \'csh$\' \'\-v sh$\')
2204? grep \-c $i /etc/passwd
2205? end
220627
2207128
2208430
2209%
2210.DE
2211Note here that the shell prompts for
2212input with `? ' when reading the body of the loop.
2213.PP
2214Very useful with loops are variables which contain lists of filenames
2215or other words.
2216You can, for example, do
2217.DS
2218% set a=(`ls`)
2219% echo $a
2220csh.n csh.rm
2221% ls
2222csh.n
2223csh.rm
2224% echo $#a
22252
2226%
2227.DE
2228The
2229.I set
2230command here gave the variable
2231.I a
2232a list of all the filenames in the current directory as value.
2233We can then iterate over these names to perform any chosen function.
2234.PP
2235The output of a command within `\`' characters is converted by
2236the shell to a list of words.
2237You can also place the `\`' quoted string within `"' characters
2238to take each (non-empty) line as a component of the variable;
2239preventing the lines from being split into words at blanks and tabs.
2240A modifier `:x' exists which can be used later to expand each component
2241of the variable into another variable splitting it into separate words
2242at embedded blanks and tabs.
2243.NH 2
2244Braces { ... } in argument expansion
2245.PP
2246Another form of filename expansion, alluded
2247to before involves the characters `{' and `}'.
2248These characters specify that the contained strings, separated by `,'
2249are to be consecutively substituted into the containing characters
2250and the results expanded left to right.
2251Thus
2252.DS
2253A{str1,str2,...strn}B
2254.DE
2255expands to
2256.DS
2257Astr1B Astr2B ... AstrnB
2258.DE
2259This expansion occurs before the other filename expansions, and may
2260be applied recursively (i.e. nested).
2261The results of each expanded string are sorted separately, left
2262to right order being preserved.
2263The resulting filenames are not required to exist if no other expansion
2264mechanisms are used.
2265This means that this mechanism can be used to generate arguments which are
2266not filenames, but which have common parts.
2267.PP
2268A typical use of this would be
2269.DS
2270mkdir ~/{hdrs,retrofit,csh}
2271.DE
2272to make subdirectories `hdrs', `retrofit' and `csh'
2273in your home directory.
2274This mechanism is most useful when the common prefix is longer
2275than in this example, i.e.
2276.DS
2277chown bin /usr/{bin/{ex,edit},lib/{ex1.1strings,how_ex}}
2278.DE
2279.NH 2
2280Command substitution
2281.PP
2282A command enclosed in `\`' characters is replaced, just before
2283filenames are expanded, by the output from that command.
2284Thus it is possible to do
2285.DS
2286set pwd=\`pwd\`
2287.DE
2288to save the current directory in the variable
2289.I pwd
2290or to do
2291.DS
2292ex \`grep -l TRACE *.c\`
2293.DE
2294to run the editor
2295.I ex
2296suppling as arguments those files whose names end in `.c'
2297which have the string `TRACE' in them.*
2298.FS
2299*Command expansion also occurs in input redirected with `<<'
2300and within `"' quotations.
2301Refer to the shell manual section for full details.
2302.FE
2303.NH 2
2304Other details not covered here
2305.PP
2306In particular circumstances it may be necessary to know the exact
2307nature and order of different substitutions performed by the shell.
2308The exact meaning of certain combinations of quotations is also
2309occasionally important.
2310These are detailed fully in its manual section.
2311.PP
2312The shell has a number of command line option flags mostly of use
2313in writing \s-2UNIX\s0 programs,
2314and debugging shell scripts.
2315See the shells manual section for a list of these options.
2316.bp
2317.SH
2318Appendix \- Special characters
2319.LP
2320The following table lists the special characters of
2321.I csh
2322and the \s-2UNIX\s0 system, giving for each the section(s) in which it
2323is discussed.
2324A number of these characters also have special meaning in expressions.
2325See the
2326.I csh
2327manual section
2328for a complete list.
2329.LP
2330Syntactic metacharacters
2331.DS
2332; 2.4 separates commands to be executed sequentially
2333| 1.5 separates commands in a pipeline
2334( ) 2.2,3.6 brackets expressions and variable values
2335& 2.5 follows commands to be executed without waiting for completion
2336.DE
2337.LP
2338Filename metacharacters
2339.DS
2340/ 1.6 separates components of a file's pathname
2341\. 1.6 separates root parts of a file name from extensions
2342? 1.6 expansion character matching any single character
2343* 1.6 expansion character matching any sequence of characters
2344[ ] 1.6 expansion sequence matching any single character from a set
2345~ 1.6 used at the beginning of a filename to indicate home directories
2346{ } 4.2 used to specify groups of arguments with common parts
2347.DE
2348.LP
2349Quotation metacharacters
2350.DS
2351\e 1.7 prevents meta-meaning of following single character
2352\' 1.7 prevents meta-meaning of a group of characters
2353" 4.3 like \', but allows variable and command expansion
2354.DE
2355.LP
2356Input/output metacharacters
2357.DS
2358< 1.3 indicates redirected input
2359> 1.5 indicates redirected output
2360.DE
2361.LP
2362Expansion/substitution metacharacters
2363.DS
2364$ 3.4 indicates variable substitution
2365! 2.3 indicates history substitution
2366: 3.6 precedes substitution modifiers
2367\(ua 2.3 used in special forms of history substitution
2368\` 4.3 indicates command substitution
2369.DE
2370.LP
2371Other metacharacters
2372.DS
2373# 3.6 begins a shell comment
2374\- 1.2 prefixes option (flag) arguments to commands
2375.DE
2376.bp
2377.SH
2378Glossary
2379.PP
2380This glossary lists the most important terms introduced in the
2381introduction to the
2382shell and gives references to sections of the shell
2383document for further information about them.
2384References of the form
2385`pr (1)'
2386indicate that the command
2387.I pr
2388is in the \s-2UNIX\s0 programmers manual in section 1.
2389You can get an online copy of its manual page by doing
2390.DS
2391man 1 pr
2392.DE
2393References of the form (2.5)
2394indicate that more information can be found in section 2.5 of this
2395manual.
2396.IP \&\fB.\fR 15n
2397Your current directory has the name `.' as well as the name printed
2398by the command
2399.I pwd.
2400The current directory `.' is usually the first component of the search
2401path contained in the variable
2402.I path,
2403thus commands which are in `.' are found first (2.2).
2404The character `.' is also used in separating components of filenames
2405(1.6).
2406The character `.' at the beginning of a component of a pathname is
2407treated specially and not matched by the filename expansion
2408metacharacters `?', `*', and `[' `]' pairs (1.6).
2409.IP \&\fB..\fR
2410Each directory has a file `..' in it which is a reference to its
2411.I parent
2412directory.
2413After changing into the directory with
2414.I chdir,
2415i.e.
2416.DS
2417chdir paper
2418.DE
2419you can return to the parent directory by doing
2420.DS
2421chdir ..
2422.DE
2423The current directory is printed by
2424.I pwd
2425(2.6).
2426.IP alias
2427An
2428.I alias
2429specifies a shorter or different name for a \s-2UNIX\s0
2430command, or a transformation on a command to be performed in
2431the shell.
2432The shell has a
2433command
2434.I alias
2435which establishes aliases and can print their current values.
2436The command
2437.I unalias
2438is used to remove aliases (2.6).
2439.IP argument
2440Commands in \s-2UNIX\s0 receive a list of argument words.
2441Thus the command
2442.DS
2443echo a b c
2444.DE
2445consists of a command name `echo' and three argument words `a', `b' and `c' (1.1).
2446.IP argv
2447The list of arguments to a command written in the shell language
2448(a shell script or shell procedure) is stored in a variable called
2449.I argv
2450within the shell.
2451This name is taken from the conventional name in the
2452C programming language (3.4).
2453.IP background
2454Commands started without waiting for them to complete are called
2455.I background
2456commands (1.5).
2457.IP bin
2458A directory containing binaries of programs and shell scripts to be
2459executed is typically called a `bin' directory.
2460The standard system `bin' directories are `/bin' containing the most
2461heavily used commands and `/usr/bin' which contains most other user
2462programs.
2463Other binaries are contained in directories such as `/usr/new' where
2464new programs are placed.
2465You can place binaries in any directory.
2466If you wish to execute them often, the name of the directories
2467should be a component of the variable
2468.I path.
2469.IP break
2470.I Break
2471is a built-in command used to exit from loops within the control
2472structure of the shell (3.6).
2473.IP builtin
2474A command executed directly by the shell is called a
2475.I builtin
2476command.
2477Most commands in \s-2UNIX\s0 are not built into the shell,
2478but rather exist as files in `bin' directories.
2479These commands are accessible because the directories in which
2480they reside are named in the
2481.I path
2482variable.
2483.IP case
2484A
2485.I case
2486command is used as a label in a
2487.I switch
2488statement in the shells control structure, similar to that of the
2489language C.
2490Details are given in the shells documentation `csh (NEW)' (3.7).
2491.IP cat
2492The
2493.I cat
2494program catenates a list of specified files on the standard output.
2495It is usually used to look at the contents of a single file on the terminal,
2496to `cat a file' (1.8, 2.3).
2497.IP cd
2498The
2499.I cd
2500command is used to change the working directory.
2501With no arguments,
2502.I cd
2503changes your working directory to be your
2504.I home
2505directory (2.3) (2.6).
2506.IP chdir
2507The
2508.I chdir
2509command is a synonym for
2510.I cd.
2511.I Cd
2512is usually used because it is easier to type.
2513.IP chsh
2514The
2515.I chsh
2516command is used to change the shell which you use on \s-2UNIX\s0.
2517By default, you use an older `standard' version of the shell
2518which resides in `/bin/sh'.
2519You can change your shell to `/bin/csh' by doing
2520.DS
2521chsh your-login-name /bin/csh
2522.DE
2523Thus I would do
2524.DS
2525chsh bill /bin/csh
2526.DE
2527It is only necessary to do this once.
2528The next time you log in to \s-2UNIX\s0 after doing this command,
2529you will be using
2530.I csh
2531rather than the shell in `/bin/sh' (1.9).
2532.IP cmp
2533.I Cmp
2534is a program which compares files.
2535It is usually used on binary files, or to see if two files are identical (3.6).
2536For comparing text files the program
2537.I diff,
2538described in `diff (1)' is used.
2539.IP command
2540A function performed by the system, either by the shell
2541(a builtin command) or by a program residing in a file in
2542a directory within the \s-2UNIX\s0 system is called a
2543.I command
2544(1.1).
2545.IP "command substitution"
2546.br
2547The replacement of a command enclosed in `\`' characters
2548by the text output by that command
2549is called
2550.I "command substitution"
2551(3.6, 4.3).
2552.IP component
2553A part of a
2554.I pathname
2555between `/' characters is called a
2556.I component
2557of that pathname.
2558A
2559.I variable
2560which has multiple strings as value is said to have
2561several
2562.I components,
2563each string is a
2564.I component
2565of the variable.
2566.IP continue
2567A builtin command which causes execution of the enclosing
2568.I foreach
2569or
2570.I while
2571loop to cycle prematurely.
2572Similar to the
2573.I continue
2574command in the programming language C (3.6).
2575.IP "core dump"
2576When a program terminates abnormally, the system places an image
2577of its current state in a file named `core'.
2578This `core dump' can be examined with the system debuggers `db (1)'
2579and `cdb (1)' in order to determine what went wrong with the program (1.8).
2580If the shell produces a message of the form:
2581.DS
2582commandname: Illegal instruction \-\- Core dumped
2583.DE
2584(where `Illegal instruction' is only one of several possible
2585messages) you should report this to the author of the program
2586and save the `core' file.
2587If this was a system program you should report this with the
2588.I trouble
2589command `trouble (1)'.
2590.IP cp
2591The
2592.I cp
2593(copy) program is used to copy the contents of one file into another
2594file.
2595It is one of the most commonly used \s-2UNIX\s0 commands (2.6).
2596.IP \&.cshrc
2597The file
2598.I \&.cshrc
2599in your
2600.I home
2601directory is read by each shell as it begins execution.
2602It is usually used to change the setting of the variable
2603.I path
2604and to set
2605.I alias
2606parameters which are to take effect globally (2.1).
2607.IP date
2608The
2609.I date
2610command prints the current date and time (1.3).
2611.IP debugging
2612.I Debugging
2613is the process of correcting mistakes in programs and shell scripts.
2614The shell has several options and variables which may be used
2615to aid in shell debugging (4.4).
2616.IP default
2617The label
2618.I default:
2619is used within shell
2620.I switch
2621statements, as it is in the C language
2622to label the code to be executed if none of the
2623.I case
2624labels matches the value switched on (3.7).
2625.IP \s-2DELETE\s0
2626The
2627\s-2DELETE\s0
2628or
2629\s-2RUBOUT\s0
2630key on the terminal is used to generate an
2631\s-2INTERRUPT\s0
2632signal in \s0UNIX\s0
2633which stops the execution of most programs (2.6).
2634.IP detached
2635A command run without waiting for it to complete is said to be detached
2636(2.5).
2637.IP diagnostic
2638An error message produced by a program is often referred to as a
2639.I diagnostic.
2640Most error messages are not written to the standard output,
2641since that is often directed away from the terminal (1.3, 1.5).
2642Error messsages are instead written to the
2643.I "diagnostic output"
2644which may be directed away from the terminal, but usually is not.
2645Thus diagnostics will usually appear on the terminal (2.5).
2646.IP directory
2647A structure which contains files.
2648At any time you are in one particular directory whose
2649names can be printed by the command `pwd'.
2650The
2651.I chdir
2652command will change you to another directory, and make the files
2653in that directory visible.
2654The directory in which you are when you first login is your
2655.I home
2656directory (1.1, 1.6).
2657.IP echo
2658The
2659.I echo
2660command prints its arguments (1.6, 2.6, 3.6, 3.10).
2661.IP else
2662The
2663.I else
2664command is part of the `if-then-else-endif' control
2665command construct (3.6).
2666.IP \s-2EOF\s0
2667An
2668.I "end-of-file"
2669is generated by the terminal by a control-d,
2670and whenever a command reads to the end of a file which
2671it has been given as input.
2672Commands receiving input from a
2673.I pipe
2674receive an end-of-file when the command sending them
2675input completes.
2676Most commands terminate when they receive an end-of-file.
2677The shell has an option to ignore end-of-file from a terminal
2678input which may help you keep from logging out accidentally
2679by typing too many control-d's (1.1, 1.8, 3.8).
2680.IP escape
2681A character \e used to prevent the special meaning of a metacharacter
2682is said to
2683.I escape
2684the character from its special meaning.
2685Thus
2686.DS
2687echo \e*
2688.DE
2689will echo the character `*' while just
2690.DS
2691echo *
2692.DE
2693will echo the names of the file in the current directory.
2694In this example, \e
2695.I escapes
2696`*' (1.7).
2697There is also a non-printing character called
2698.I escape,
2699usually labelled
2700\s-2ESC\s0
2701or
2702\s-2ALTMODE\s0
2703on terminal keyboards.
2704Some \s-2UNIX\s0 systems use this character to indicate that
2705output is to be suspended.
2706Other systems use control-s.
2707.IP /etc/passwd
2708This file contains information about the accounts currently on the
2709system.
2710If consists of a line for each account with fields separated by
2711`:' characters (2.3).
2712You can look at this file by saying
2713.DS
2714cat /etc/passwd
2715.DE
2716The command
2717.I grep
2718is often used to search for information in this file.
2719See `passwd (5)' and `grep (1)' for more details.
2720.IP exit
2721The
2722.I exit
2723command is used to force termination of a shell script,
2724and is built into the shell (3.9).
2725.IP "exit status"
2726A command which discovers a problem may reflect this back to the command
2727(such as a shell) which invoked (executed) it.
2728It does this by returning a non-zero number as its
2729.I "exit status,"
2730a status of zero being considered
2731`normal termination'.
2732The
2733.I exit
2734command can be used to force a shell command script to give a non-zero
2735exit status (3.5).
2736.IP expansion
2737The replacement of strings in the shell input which contain metacharacters
2738by other strings is referred to as the process of
2739.I expansion.
2740Thus the replacement of the word `*' by a sorted list of files
2741in the current directory is a `filename expansion'.
2742Similarly the replacement of the characters `!!' by the text of
2743the last command is a `history expansion'.
2744Expansions are also referred to as
2745.I substitutions
2746(1.6, 3.4, 4.2).
2747.IP expressions
2748Expressions are used in the shell
2749to control the conditional structures used in the writing of shell
2750scripts and in calculating values for these scripts.
2751The operators available in shell expressions are those of the language
2752C (3.5).
2753.IP extension
2754Filenames often consist of a
2755.I root
2756name and an
2757.I extension
2758separated by the character `.'.
2759By convention, groups of related files often share the same root name.
2760Thus if `prog.c' were a C program, then the object file for this
2761program would be stored in `prog.o'.
2762Similarly a paper written with the
2763`\-me'
2764nroff macro package might be stored in
2765`paper.me'
2766while a formatted version of this paper might be kept in
2767`paper.out' and a list of spelling errors in
2768`paper.errs' (1.6).
2769.IP filename
2770Each file in \s-2UNIX\s0 has a name consisting of up to 14 characters
2771and not including the character `/' which is used in
2772.I pathname
2773building.
2774Most file names do not begin with the character `.', and contain
2775only letters and digits with perhaps a `.' separating the root
2776portion of the filename from an extension (1.6).
2777.IP "filename expansion"
2778.br
2779Filename expansion uses the metacharacters `*', `?' and `[' and `]'
2780to provide a convenient mechanism for naming files.
2781Using filename expansion it is easy to name all the files in
2782the current directory, or all files which have a common root name.
2783Other filename expansion mechanisms use the metacharacter `~' and allow
2784files in other users directories to be named easily (1.6, 4.2).
2785.IP flag
2786Many \s-2UNIX\s0 commands accept arguments which are not the names
2787of files or other users but are used to modify the action of the commands.
2788These are referred to as
2789.I flag
2790options, and by convention consists of one or more letters preceded by
2791the character `\-' (1.2).
2792Thus the
2793.I ls
2794list file commands has an option
2795`\-s' to list the sizes of files.
2796This is specified
2797.DS
2798ls \-s
2799.DE
2800.IP foreach
2801The
2802.I foreach
2803command is used in shell scripts and at the terminal to specify
2804repitition of a sequence of commands while the value of a certain
2805shell variable ranges through a specified list (3.6, 4.1).
2806.IP getty
2807The
2808.I getty
2809program is part of the system which determines the speed at which
2810your terminal is to run when you first log in.
2811It types the initial system banner and `login:'.
2812When no one is logged in on a terminal a
2813.I ps
2814command shows a command of the form
2815`- 7'
2816where `7' here is often some other single letter or digit.
2817This `7' is an option to the
2818.I getty
2819command, indicating the type of port which it is running on.
2820If you see a
2821.I getty
2822command running on a terminal in the output of
2823.I ps
2824you know that no one is logged in on that terminal (2.3).
2825.IP goto
2826The shell has a command
2827.I goto
2828used in shell scripts to transfer control to a given label (3.7).
2829.IP grep
2830The
2831.I grep
2832command searches through a list of argument files for a specified string.
2833Thus
2834.DS
2835grep bill /etc/passwd
2836.DE
2837will print each line in the file
2838`/etc/passwd'
2839which contains the string `bill'.
2840Actually,
2841.I grep
2842scans for
2843.I "regular expressions"
2844in the sense of the editors
2845`ed (1)' and `ex (1)' (2.3).
2846.I Grep
2847stands for
2848`globally find regular expression and print.'
2849.IP hangup
2850When you hangup a phone line, a \s-2HANGUP\s0 signal
2851is sent to all running processes on your terminal, causing
2852them to terminate execution prematurely. If you wish
2853to start commands to run after you log off a dialup you must
2854use the command
2855.I nohup
2856(2.6).
2857.IP head
2858The
2859.I head
2860command prints the first few lines of one or more files.
2861If you have a bunch of files containing text which you are wondering
2862about it is sometimes is useful to run
2863.I head
2864with these files as arguments.
2865This will usually show enough of what is in these files to let you decide
2866which you are interested in (1.5, 2.3).
2867.IP history
2868The
2869.I history
2870mechanism of the shell allows previous commands to be repeated,
2871possibly after modification to correct typing mistakes or to change
2872the meaning of the command.
2873The shell has a
2874.I "history list"
2875where these commands are kept, and a
2876.I history
2877variable which controls how large this list is (1.7, 2.6).
2878.IP "home directory"
2879Each user has a home directory, which is given in your entry
2880in the password file,
2881.I /etc/passwd.
2882This is the directory which you are placed in when you first log in.
2883The
2884.I cd
2885or
2886.I chdir
2887command with no arguments takes you back to this directory, whose
2888name is recorded in the shell variable
2889.I home.
2890You can also access the home directories of other users in forming
2891filenames using a file expansion notation and the character `~' (1.6).
2892.IP if
2893A conditional command within the shell, the
2894.I if
2895command is used in shell command scripts to make decisions
2896about what course of action to take next (3.6).
2897.IP ignoreeof
2898Normally, your shell will exit, printing
2899`logout'
2900if you type a control-d at a prompt of `% '.
2901This is the way you usually log off the system.
2902You can
2903.I set
2904the
2905.I ignoreeof
2906variable if you wish in your
2907.I \&.login
2908file and then use the command
2909.I logout
2910to logout.
2911This is useful if you sometimes accidentally type too many control-d
2912characters, logging yourself off.
2913If the system is slow, this can waste much time, as it may
2914take a long time to log in again (2.2, 2.6).
2915.IP input
2916Many commands on \s-2UNIX\s0 take information from the terminal or from
2917files which they then act on.
2918This information is called
2919.I input.
2920Commands normally read for input from their
2921.I "standard input"
2922which is, by default, the terminal.
2923This standard input can be redirected from a file using a shell metanotation
2924with the character `<'.
2925Many commands will also read from a file specified as argument.
2926Commands placed in pipelines will read from the output of the previous
2927command in the pipeline.
2928The leftmost command in a pipeline reads from the terminal if
2929you neither redirect its input nor give it a file name to use as
2930standard input.
2931Special mechanisms exist for suppling input to commands in shell
2932scripts (1.2, 1.6, 3.8).
2933.IP interrupt
2934An
2935.I interrupt
2936is a signal to a program that is generated by hitting the
2937\s-2RUBOUT\s0 or \s-2DELETE\s0 key.
2938It causes most programs to stop execution.
2939Certain programs such as the shell and the editors
2940handle an interrupt in special ways, usually by stopping what they
2941are doing and prompting for another command.
2942While the shell is executing another command and waiting for it
2943to finish, the shell does not listen to interrupts.
2944The shell often wakes up when you hit interrupt because many commands
2945die when they receive an interrupt (1.8, 2.6, 3.9).
2946.IP kill
2947A program which terminates processes run without waiting for them to
2948complete. (2.6)
2949.IP \&.login
2950The file
2951.I \&.login
2952in your
2953.I home
2954directory is read by the shell each time you log in to \s-2UNIX\s0
2955and the commands there are executed.
2956There are a number of commands which are usefully placed here
2957especially
2958.I tset
2959commands and
2960.I set
2961commands to the shell itself (2.1).
2962.IP logout
2963The
2964.I logout
2965command causes a login shell to exit.
2966Normally, a login shell will exit when you hit control-d
2967generating an end-of-file, but if you have set
2968.I ignoreeof
2969in you
2970.I \&.login
2971file then this will not work and you must use
2972.I logout
2973to log off the \s-2UNIX\s0 system (2.2).
2974.IP \&.logout
2975When you log off of \s-2UNIX\s0 the shell will execute commands from
2976the file
2977.I \&.logout
2978in your
2979.I home
2980directory after it prints `logout'.
2981.IP lpr
2982The command
2983.I lpr
2984is the line printer daemon.
2985The standard input of
2986.I lpr
2987is spooled and printed on the \s-2UNIX\s0 line printer.
2988You can also give
2989.I lpr
2990a list of filenames as arguments to be printed.
2991It is most common to use
2992.I lpr
2993as the last component of a
2994.I pipeline
2995(2.3).
2996.IP ls
2997The
2998.I ls
2999list files command is one of the most commonly used \s-2UNIX\s0
3000commands.
3001With no argument filenames it prints the names of the files in the
3002current directory.
3003It has a number of useful
3004.I flag
3005arguments, and can also be given the names of directories
3006as arguments, in which case it lists the names of the files in these
3007directories (1.2).
3008.IP mail
3009The
3010.I mail
3011program is used to send and receive messages from other \s-2UNIX\s0
3012users (1.1, 2.2).
3013.IP make
3014The
3015.I make
3016command is used to maintain one or more related files and to
3017organize functions to be performed on these files.
3018In many ways
3019.I make
3020is easier to use, and more helpful
3021than
3022shell command scripts (3.2).
3023.IP makefile
3024The file containing command for
3025.I make
3026is called
3027`makefile' (3.2).
3028.IP manual
3029The `manual' often referred to is the
3030`\s-2UNIX\s0 programmers manual.'
3031It contains a number of sections and a description of each \s-2UNIX\s0
3032program.
3033An online version of the manual is accessible through the
3034.I man
3035command.
3036Its documentation can be obtained online via
3037.DS
3038man man
3039.DE
3040.IP metacharacter
3041Many characters which are neither letters nor digits have special meaning
3042either to the shell or to \s-2UNIX\s0.
3043These characters are called
3044.I metacharacters.
3045If it is necessary to place these characters in arguments to commands
3046without them having their special meaning then they must be
3047.I quoted.
3048An example of a metacharacter is the character `>' which is used
3049to indicate placement of output into a file.
3050For the purposes of the
3051.I history
3052mechanism,
3053most unquoted metacharacters form separate words (1.4).
3054The appendix to this user's manual lists the metacharacters
3055in groups by their function.
3056.IP mkdir
3057The
3058.I mkdir
3059command is used to create a new directory (2.6).
3060.IP modifier
3061Substitutions with the history mechanism, keyed by the character `!'
3062or of variables using the metacharacter `$' are often subjected
3063to modifications, indicated by placing the character `:' after the
3064substitution and following this with the modifier itself.
3065The
3066.I "command substitution"
3067mechanism can also be used to perform modification in a similar way,
3068but this notation is less clear (3.6).
3069.IP noclobber
3070The shell has a variable
3071.I noclobber
3072which may be set in the file
3073.I \&.login
3074to prevent accidental destruction of files by the `>' output redirection
3075metasyntax of the shell (2.2, 2.5).
3076.IP nohup
3077A shell command used to allow background commands to run to completion
3078even if you log off a dialup before they complete. (2.5)
3079.IP nroff
3080The standard text formatter on \s-2UNIX\s0 is the program
3081.I nroff.
3082Using
3083.I nroff
3084and one of the available
3085.I macro
3086packages for it, it is possible to have documents automatically
3087formatted and to prepare them for phototypesetting using the
3088typesetter program
3089.I troff
3090(3.2).
3091.IP onintr
3092The
3093.I onintr
3094command is built into the shell and is used to control the action
3095of a shell command script when an interrupt signal is received (3.9).
3096.IP output
3097Many commands in \s-2UNIX\s0 result in some lines of text which are
3098called their
3099.I output.
3100This output is usually placed on what is known as the
3101.I "standard output"
3102which is normally connected to the users terminal.
3103The shell has a syntax using the metacharacter `>' for redirecting
3104the standard output of a command to a file (1.3).
3105Using the
3106.I pipe
3107mechanism and the metacharacter `|' it is also possible for
3108the standard output of one command to become the standard input of another
3109command (1.5).
3110Certain commands such as the line printer daemon
3111.I lpr
3112do not place their results on the standard output but rather in more
3113useful places such as on the line printer (2.3).
3114Similarly the
3115.I write
3116command places its output on another users terminal rather than its
3117standard output (2.3).
3118Commands also have a
3119.I "diagnostic output"
3120where they write their error messages.
3121Normally these go to the terminal even if the standard output has
3122been sent to a file or another command, but it is possible
3123to direct error diagnostics along with standard output using
3124a special metanotation (2.5).
3125.IP path
3126The shell has a variable
3127.I path
3128which gives the names of the directories in which it searches for
3129the commands which it is given.
3130It always checks first to see if the command it is given is
3131built into the shell.
3132If it is, then it need not search for the command as it can do it internally.
3133If the command is not builtin, then the shell searches for a file
3134with the name given in each of the directories in the
3135.I path
3136variable, left to right.
3137Since the normal definition of the
3138.I path
3139variable is
3140.DS
3141path (. /bin /usr/bin)
3142.DE
3143the shell normally looks in the current directory, and then in
3144the standard system directories `/bin' and `/usr/bin' for the named
3145command (2.2).
3146If the command cannot be found the shell will print an error diagnostic.
3147Scripts of shell commands will be executed using another shell to interpret
3148them if they have `execute' bits set.
3149This is normally true because a command of the form
3150.DS
3151chmod 755 script
3152.DE
3153was executed to turn these execute bits on (3.3).
3154.IP pathname
3155A list of names, separated by `/' characters forms a
3156.I pathname.
3157Each
3158.I component,
3159between successive `/' characters, names a directory
3160in which the next component file resides.
3161Pathnames which begin with the character `/' are interpreted relative
3162to the
3163.I root
3164directory in the filesystem.
3165Other pathnames are interpreted relative to the current directory
3166as reported by
3167.I pwd.
3168The last component of a pathname may name a directory, but
3169usually names a file.
3170.IP pipeline
3171A group of commands which are connected together, the standard
3172output of each connected to the standard input of the next
3173is called a
3174.I pipeline.
3175The
3176.I pipe
3177mechanism used to connect these commands is indicated by
3178the shell metacharacter `|' (1.5, 2.3).
3179.IP pr
3180The
3181.I pr
3182command is used to prepare listings of the contents of files
3183with headers giving the name of the file and the date and
3184time at which the file was last modified (2.3).
3185.IP printenv
3186The
3187.I printenv
3188command is used on version 7 \s-2UNIX\s0 systems
3189to print the current setting of variables in the environment.
3190As of this writing, only the \s-2VAX/UNIX\s0 system on the fifth
3191floor of Evans Hall is running a version 7 \s-2UNIX\s0 system.
3192The other systems are running version 6, which does not have
3193or need
3194.I printenv
3195(2.6).
3196.IP process
3197A instance of a running program is called a process (2.6).
3198The
3199numbers used by
3200.I kill
3201and printed by
3202.I wait
3203are unique numbers generated for these processes by \s-2UNIX\s0.
3204They are useful in
3205.I kill
3206commands which can be used to stop background processes. (2.6)
3207.IP program
3208Usually synonymous with
3209.I command;
3210a binary file or shell command script
3211which performs a useful function is often
3212called a program.
3213.IP "programmers manual"
3214.br
3215Also referred to as the
3216.I manual.
3217See the glossary entry for `manual'.
3218.IP prompt
3219Many programs will print a prompt on the terminal when they expect
3220input.
3221Thus the editor
3222`ex (NEW)' will print a `:' when it expects input.
3223The shell prompts for input with `% ' and occasionally with `? ' when
3224reading commands from the terminal (1.1).
3225The shell has a variable
3226.I prompt
3227which may be set to a different value to change the shells main prompt.
3228This is mostly used when debugging the shell (2.6).
3229.IP ps
3230The
3231.I ps
3232command is used to show the processes you are currently running.
3233Each process is shown with its unique process number,
3234an indication of the terminal name it is attached to,
3235and the amount of \s-2CPU\s0 time it has used so far.
3236The command is identified by printing some of the words used
3237when it was invoked (2.3, 2.6).
3238Login shells, such as the
3239.I csh
3240you get when you login are shown as `\-'.
3241.IP pwd
3242The
3243.I pwd
3244command prints the full pathname of the current (working)
3245directory.
3246.IP quit
3247The
3248.I quit
3249signal,
3250generated by a control-\e
3251is used to terminate programs which are behaving unreasonably.
3252It normally produces a core image file (1.8).
3253.IP quotation
3254The process by which metacharacters are prevented their special
3255meaning, usually by using the character `\' in pairs, or by
3256using the character `\e' is referred to as
3257.I quotation
3258(1.4).
3259.IP redirection
3260The routing of input or output from or to a file is known
3261as
3262.I redirection
3263of input or output (1.3).
3264.IP repeat
3265The
3266.I repeat
3267command iterates another command a specified number of times (2.6).
3268.IP \s-2RUBOUT\s0
3269The \s-2RUBOUT\s0 or \s-2DELETE\s0
3270key generates an interrupt signal which is used to stop programs
3271or to cause them to return and prompt for more input (2.6).
3272.IP script
3273Sequences of shell commands placed in a file are called shell command
3274scripts.
3275It is often possible to perform simple tasks using these scripts without
3276writing a program in a language such as C, by
3277using the shell to selectively run other programs (3.2, 3.3, 3.10).
3278.IP set
3279The builtin
3280.I set
3281command is used to assign new values to shell variables
3282and to show the values of the current variables.
3283Many shell variables have special meaning to the shell itself.
3284Thus by using the set command the behavior of the shell can be affected (2.1).
3285.IP setenv
3286On version 7 systems variables in the environment `environ (5)'
3287can be changed by using the
3288.I setenv
3289builtin command (2.6).
3290The
3291.I printenv
3292command can be used to print the value of the variables in the environment.
3293Currently, only the \s-2VAX/UNIX\s0 system on the fifth floor of Evans Hall
3294is running version 7 \s-2UNIX\s0.
3295The other systems are running version 6, where
3296.I setenv
3297is not necessary and does not exist.
3298.IP shell
3299A shell is a command language interpreter.
3300It is possible to write and run your own shell,
3301as shells are no different than any other programs as far as the
3302system is concerned.
3303This manual deals with the details of one particular shell,
3304called
3305.I csh.
3306.IP "shell script"
3307See
3308.I script
3309(3.2, 3.3, 3.10).
3310.IP sort
3311The
3312.I sort
3313program sorts a sequence of lines in ways that can be controlled
3314by argument flags (1.5).
3315.IP source
3316The
3317.I source
3318command causes the shell to read commands from a specified file.
3319It is most useful for reading files such as
3320.I \&.cshrc
3321after changing them (2.6).
3322.IP "special character"
3323.br
3324See
3325.I metacharacters
3326and the
3327appendix to this manual.
3328.IP standard
3329We refer often to the
3330.I "standard input"
3331and
3332.I "standard output"
3333of commands.
3334See
3335.I input
3336and
3337.I output
3338(1.3, 3.8).
3339.IP status
3340A command normally returns a
3341.I status
3342when it finishes.
3343By convention a
3344.I status
3345of zero indicates that the command succeeded.
3346Commands may return non-zero status to indicate that some abnormal
3347event has occurred.
3348The shell variable
3349.I status
3350is set to the status returned by the last command.
3351It is most useful in shell commmand scripts (3.5, 3.6).
3352.IP substitution
3353The shell implements a number of
3354.I substitutions
3355where sequences indicated by metacharacters are replaced by other sequences.
3356Notable examples of this are history substitution keyed by the
3357metacharacter `!' and variable substitution indicated by `$'.
3358We also refer to substitutions as
3359.I expansions
3360(3.4).
3361.IP switch
3362The
3363.I switch
3364command of the shell allows the shell
3365to select one of a number of sequences of commands based on an
3366argument string.
3367It is similar to the
3368.I switch
3369statement in the language C (3.7).
3370.IP termination
3371When a command which is being executed finished we say it undergoes
3372.I termination
3373or
3374.I terminates.
3375Commands normally terminate when they read an end-of-file
3376from their standard input.
3377It is also possible to terminate commands by sending them
3378an
3379.I interrupt
3380or
3381.I quit
3382signal (1.8).
3383The
3384.I kill
3385program terminates specified command whose numbers are given (2.6).
3386.IP then
3387The
3388.I then
3389command is part of the shells
3390`if-then-else-endif' control construct used in command scripts (3.6).
3391.IP time
3392The
3393.I time
3394command can be used to measure the amount of \s-2CPU\s0
3395and real time consumed by a specified command (2.1, 2.6).
3396.IP troff
3397The
3398.I troff
3399program is used to typeset documents.
3400See also
3401.I nroff
3402(3.2).
3403.IP tset
3404The
3405.I tset
3406program is used to set standard erase and kill characters
3407and to tell the system what kind of terminal you are using.
3408It is often invoked in a
3409.I \&.login
3410file (2.1).
3411.IP unalias
3412The
3413.I unalias
3414command removes aliases (2.6).
3415.IP \s-2UNIX\s0
3416\s-2UNIX\s0 is an operating system on which
3417.I csh
3418runs.
3419\s-2UNIX\s0 provides facilities which allow
3420.I csh
3421to invoke other programs such as editors and text formatters which
3422you may wish to use.
3423.IP unset
3424The
3425.I unset
3426command removes the definitions of shell variables (2.2, 2.6).
3427.IP "variable expansion"
3428.br
3429See
3430.I variables
3431and
3432.I expansion
3433(2.2, 3.4).
3434.IP variables
3435Variables in
3436.I csh
3437hold one or more strings as value.
3438The most common use of variables is in controlling the behavior
3439of the shell.
3440See
3441.I path,
3442.I noclobber,
3443and
3444.I ignoreeof
3445for examples.
3446Variables such as
3447.I argv
3448are also used in writing shell programs (shell command scripts)
3449(2.2).
3450.IP verbose
3451The
3452.I verbose
3453shell variable can be set to cause commands to be echoed
3454after they are history expanded.
3455This is often useful in debugging shell scripts.
3456The
3457.I verbose
3458variable is set by the shells
3459.I \-v
3460command line option (3.10).
3461.IP wait
3462The builtin command
3463.I wait
3464causes the shell to pause, and not prompt,
3465until all commands run in the background have terminated (2.6).
3466.IP where
3467The
3468.I where
3469command shows where the users named as arguments are logged into the
3470system (2.3).
3471.IP while
3472The
3473.I while
3474builtin control construct is used in shell command scripts (3.7).
3475.IP word
3476A sequence of characters which forms an argument to a command is called
3477a
3478.I word.
3479Many characters which are neither letters, digits, `\-', `.' or `/'
3480form words all by themselves even if they are not surrounded
3481by blanks.
3482Any sequence of character may be made into a word by surrounding it
3483with `\'' characters
3484except for the characters `\'' and `!' which require special treatment
3485(1.1, 1.6).
3486This process of placing special characters
3487in words without their special meaning is called
3488.I quoting.
3489.IP "working directory"
3490.br
3491At an given time you are in one particular directory, called
3492your working directory.
3493This directories name is printed by the
3494.I pwd
3495command and the files listed by
3496.I ls
3497are the ones in this directory.
3498You can change working directories using
3499.I chdir.
3500.IP write
3501The
3502.I write
3503command is used to communicate with other users who are logged in to
3504\s-2UNIX\s0 (2.3).