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