BSD 2 development
[unix-history] / man / csh.u
CommitLineData
1d69d2db
BJ
1.if n .ds ua \o'^|'
2.if t .ds ua \(ua
3.if n .ds aa '
4.if t .ds aa \(aa
5.if n .ds ga `
6.if t .ds ga \(ga
7.if t .tr *\(**
8.TH CSH UCB 2/24/79 UCB
9.SH NAME
10csh \- a shell (command interpreter) with C-like syntax
11.SH SYNOPSIS
12.B csh
13[
14.B \-cef\^instvVxX
15] [
16arg ...
17]
18.SH DESCRIPTION
19.I Csh
20is a command language interpreter.
21It begins by executing commands from the file `.cshrc'
22in the
23.I home
24directory of the invoker.
25If this is a login shell then it also executes commands from the file
26`.login' there.
27In the normal case, the shell will then begin reading commands from the
28terminal, prompting with `% '.
29Processing of arguments and the use of the shell to process files
30containing command scripts will be described later.
31.PP
32The shell then repeatedly performs the following actions:
33a line of command input is read and broken into
34.I words.
35This sequence of words is placed on the command history list and then parsed.
36Finally each command in the current line is executed.
37.PP
38When a login shell terminates it executes commands from the file `.logout'
39in the users home directory.
40.PP
41.B "Lexical structure"
42.PP
43The shell splits input lines into words at blanks and tabs with the
44following exceptions.
45The characters
46`&' `|' `;' `<' `>' `(' `)'
47form separate words.
48If doubled in `&&', `|\|\||', `<<' or `>>' these pairs form single words.
49These parser metacharacters may be made part of other words, or prevented their
50special meaning, by preceding them with `\e'.
51A newline preceded by a `\e' is equivalent to a blank.
52.PP
53In addition strings enclosed in matched pairs of quotations,
54`\*(aa', `\*(ga' or `"',
55form parts of a word; metacharacters in these strings, including blanks
56and tabs, do not form separate words.
57These quotations have semantics to be described subsequently.
58Within pairs of `\' or `"' characters a newline preceded by a `\e' gives
59a true newline character.
60.PP
61When the shell's input is not a terminal,
62the character `#' introduces a comment which continues to the end of the
63input line.
64It is prevented this special meaning when preceded by `\e'
65and in quotations using `\`', `\'', and `"'.
66.PP
67.B Commands
68.PP
69A simple command is a sequence of words, the first of which
70specifies the command to be executed.
71A simple command or
72a sequence of simple commands separated by `|' characters
73forms a pipeline.
74The output of each command in a pipeline is connected to the input of the next.
75Sequences of pipelines may be separated by `;', and are then executed
76sequentially.
77A sequence of pipelines may be executed without waiting for it to terminate
78by following it with an `&'.
79.PP
80Any of the above may be placed in `(' `)' to form a simple command (which
81may be a component of a pipeline, etc.)
82It is also possible to separate pipelines with `|\|\||' or `&&' indicating,
83as in the C language,
84that the second is to be executed only if the first fails or succeeds
85respectively. (See
86.I Expressions.)
87.PP
88.B Substitutions
89.PP
90We now describe the various transformations the shell performs on the
91input in the order in which they occur.
92.PP
93.B "History substitutions"
94.PP
95History substitutions can be used to reintroduce sequences of words
96from previous commands, possibly performing modifications on these words.
97Thus history substitutions provide a generalization of a
98.I redo
99function.
100.PP
101History substitutions begin with the character `!' and may begin
102.B anywhere
103in the input stream if a history substitution is not already in progress.
104This `!' may be preceded by an `\e' to prevent its special meaning;
105a `!' is passed unchanged when it is followed by a blank,
106tab, newline, `=' or `('.
107History substitutions also occur when an input line begins with `\*(ua'.
108This special abbreviation will be described later.
109.PP
110Any input line which contains history substitution is echoed on the terminal
111before it is executed as it could have been typed without history substitution.
112.PP
113Commands input from the terminal which consist of one or more words
114are saved on the history list,
115the size of which is controlled by the
116.I history
117variable.
118The previous command is always retained.
119Commands are numbered sequentially from 1.
120.PP
121For definiteness, consider the following output from the history command:
122.PP
123.DT
124.br
125 \09 write michael
126.br
127 10 ex write.c
128.br
129 11 cat oldwrite.c
130.br
131 12 diff *write.c
132.PP
133The commands are shown with their event numbers.
134It is not usually necessary to use event numbers, but the current event
135number can be made part of the
136.I prompt
137by placing an `!' in the prompt string.
138.PP
139With the current event 13 we can refer to previous events by event
140number `!11', relatively as in `!\-2' (referring to the same event),
141by a prefix of a command word
142as in `!d' for event 12 or `!w' for event 9, or by a string contained in
143a word in the command as in `!?mic?' also referring to event 9.
144These forms, without further modification, simply reintroduce the words
145of the specified events, each separated by a single blank.
146As a special case `!!' refers to the previous command; thus `!!'
147alone is essentially a
148.I redo.
149.PP
150To select words from an event we can follow the event specification by
151a `:' and a designator for the desired words.
152The words of a input line are numbered from 0,
153the first (usually command) word being 0, the second word (first argument)
154being 1, etc.
155The basic word designators are:
156.PP
157.DT
158.nf
159 0 first (command) word
160 \fIn\fR \fIn\fR\|'th argument
161 \*(ua first argument, i.e. `1'
162 $ last argument
163 % word matched by (immediately preceding) ?\fIs\fR\|? search
164 \fIx\fR\|\-\fIy\fR range of words
165 \-\fIy\fR abbreviates `0\-\fIy\fR\|'
166 * abbreviates `\*(ua\-$', or nothing if only 1 word in event
167 \fIx\fR\|* abbreviates `\fIx\fR\|\-$'
168 \fIx\fR\|\- like `\fIx\fR\|*' but omitting word `$'
169.fi
170.PP
171The `:' separating the event specification from the word designator
172can be omitted if the argument selector begins with a `\*(ua', `$', `*'
173`\-' or `%'.
174After the optional word designator can be
175placed a sequence of modifiers, each preceded by a `:'.
176The following modifiers are defined:
177.DT
178.PP
179.nf
180 h Remove a trailing pathname component, leaving the head.
181 r Remove a trailing `.xxx' component, leaving the root name.
182 s/\fIl\fR\|/\fIr\fR\|/ Substitute \fIr\fR for \fIr\fR
183 t Remove all leading pathname components, leaving the tail.
184 & Repeat the previous substitution.
185 g Apply the change globally, prefixing the above, e.g. `g&'.
186 p Print the new command but do not execute it.
187 q Quote the substituted words, preventing further substitutions.
188 x Like q, but break into words at blanks, tabs and newlines.
189.fi
190.PP
191Unless preceded by a `g' the modification is applied only to the first
192modifiable word. In any case it is an error for no word to be applicable.
193.PP
194The left hand side of substitutions are not regular expressions in the sense
195of the editors, but rather strings.
196Any character may be used as the delimiter in place of `/';
197a `\e' quotes the delimiter into the
198.IR l ""
199and
200.IR r ""
201strings.
202The character `&' in the right hand side is replaced by the text from
203the left.
204A `\e' quotes `&' also.
205A null
206.IR l ""
207uses the previous string either from a
208.IR l ""
209or from a
210contextual scan string
211.IR s ""
212in `!?\fIs\fR\|?'.
213The trailing delimiter in the substitution may be omitted if a newline
214follows immediately as may the trailing `?' in a contextual scan.
215.PP
216A history reference may be given without an event specification, e.g. `!$'.
217In this case the reference is to the previous command unless a previous
218history reference occurred on the same line in which case this form repeats
219the previous reference.
220Thus `!?foo?\*(ua !$' gives the first and last arguments
221from the command matching `?foo?'.
222.PP
223A special abbreviation of a history reference occurs when the first
224non-blank character of an input line is a `\*(ua'.
225This is equivalent to `!:s\*(ua' providing a convenient shorthand for substitutions
226on the text of the previous line.
227Thus `\*(ualb\*(ualib' fixes the spelling of
228`lib'
229in the previous command.
230Finally, a history substitution may be surrounded with `{' and `}'
231if necessary to insulate it from the characters which follow.
232Thus, after `ls \-ld ~paul' we might do `!{l}a' to do `ls \-ld ~paula',
233while `!la' would look for a command starting `la'.
234.PP
235.if n .ul
236\fBQuotations\ with\ \'\ and\ "\fR
237.PP
238The quotation of strings by `\'' and `"' can be used
239to prevent all or some of the remaining substitutions.
240Strings enclosed in `\'' are prevented any further interpretation.
241Strings enclosed in `"' are yet variable and command expanded
242as described below.
243.PP
244In both cases the resulting text becomes (all or part of) a single word;
245only in one special case (see
246.I "Command Substitition"
247below) does a `"' quoted string yield parts of more than one word;
248`\' quoted strings never do.
249.PP
250.B "Alias substitution"
251.PP
252The shell maintains a list of aliases which can be established, displayed
253and modified by the
254.I alias
255and
256.I unalias
257commands.
258After a command line is scanned, it is parsed into distinct commands and
259the first word of each command, left-to-right, is checked to see if it
260has an alias.
261If it does, then the text which is the alias for that command is reread
262with the history mechanism available
263as though that command were the previous input line.
264The resulting words replace the
265command and argument list.
266If no reference is made to the history list, then the argument list is
267left unchanged.
268.PP
269Thus if the alias for `ls' is `ls \-l' the command `ls /usr' would map to
270`ls \-l /usr', the argument list here being undisturbed.
271Similarly if the alias for `lookup' was `grep !\*(ua /etc/passwd' then
272`lookup bill' would map to `grep bill /etc/passwd'.
273.PP
274If an alias is found, the word transformation of the input text
275is performed and the aliasing process begins again on the reformed input line.
276Looping is prevented if the first word of the new text is the same as the old
277by flagging it to prevent further aliasing.
278Other loops are detected and cause an error.
279.PP
280Note that the mechanism allows aliases to introduce parser metasyntax.
281Thus we can `alias print \'pr \e!* \||\| lpr\'' to make a command which
282.I pr's
283its arguments to the line printer.
284.PP
285.B "Variable substitution"
286.PP
287The shell maintains a set of variables, each of which has as value a list
288of zero or more words.
289Some of these variables are set by the shell or referred to by it.
290For instance, the
291.I argv
292variable is an image of the shell's argument list, and words of this
293variable's value are referred to in special ways.
294.PP
295The values of variables may be displayed and changed by using the
296.I set
297and
298.I unset
299commands.
300Of the variables referred to by the shell a number are toggles;
301the shell does not care what their value is,
302only whether they are set or not.
303For instance, the
304.I verbose
305variable is a toggle which causes command input to be echoed.
306The setting of this variable results from the
307.B \-v
308command line option.
309.PP
310Other operations treat variables numerically.
311The `@' command permits numeric calculations to be performed and the result
312assigned to a variable.
313Variable values are, however, always represented as (zero or more) strings.
314For the purposes of numeric operations, the null string is considered to be
315zero, and the second and subsequent words of multiword values are ignored.
316.PP
317After the input line is aliased and parsed, and before each command
318is executed, variable substitution
319is performed keyed by `$' characters.
320This expansion can be prevented by preceding the `$' with a `\e' except
321within `"'s where it
322.B always
323occurs, and within `\'s where it
324.B never
325occurs.
326Strings quoted by `\*(ga' are interpreted later (see
327.I "Command substitution"
328below) so `$' substitution does not occur there until later, if at all.
329A `$' is passed unchanged if followed by a blank, tab, or end-of-line.
330.PP
331Input/output redirections are recognized before variable expansion,
332and are variable expanded separately.
333Otherwise, the command name and entire argument list are expanded together.
334It is thus possible for the first (command) word to this point to generate
335more than one word, the first of which becomes the command name,
336and the rest of which become arguments.
337.PP
338Unless enclosed in `"' or given the `:q' modifier the results of variable
339substitution may eventually be command and filename substituted.
340Within `"' a variable whose value consists of multiple words expands to a
341(portion of) a single word, with the words of the variables value
342separated by blanks.
343When the `:q' modifier is applied to a substitution
344the variable will expand to multiple words with each word separated
345by a blank and quoted to prevent later command or filename substitution.
346.PP
347The following metasequences are provided for introducing variable values into
348the shell input.
349Except as noted, it is an error to reference a variable which is not set.
350.HP 5
351$name
352.br
353.ns
354.HP 5
355${name}
356.br
357Are replaced by the words of the value of variable
358.I name,
359each separated by a blank.
360Braces insulate
361.I name
362from following characters which would otherwise be part of it.
363Shell variables have names consisting of up to 20 letters
364and underscores.
365.HP 5
366$name[selector]
367.br
368.ns
369.HP 5
370${name[selector]}
371.br
372May be used to select only some of the words from the value of
373.I name.
374The selector is subjected to `$' substitution and may consist of a single
375number or two numbers separated by a `\-'.
376The first word of a variables value is numbered `1'.
377If the first number of a range is omitted it defaults to `1'.
378If the last member of a range is omitted it defaults to `$#name'.
379The selector `*' selects all words.
380It is not an error for a range to be empty if the second argument is omitted
381or in range.
382.HP 5
383$#name
384.br
385.ns
386.HP 5
387${#name}
388.br
389Gives the number of words in the variable.
390This is useful for later use in a `[selector]'.
391.HP 5
392$0
393.br
394Substitutes the name of the file from which command input is being read.
395An error occurs if the name is not known.
396.HP 5
397$number
398.br
399.ns
400.HP 5
401${number}
402.br
403Equivalent to `$argv[number]'.
404.HP 5
405$*
406.br
407Equivalent to `$argv[*]'.
408.PP
409The modifiers `:h', `:t', `:r', `:q' and `:x' may be applied to
410the substitutions above as may `:gh', `:gt' and `:gr'.
411If braces `{' '}' appear in the command form then the modifiers
412must appear within the braces.
413.B "The current implementation allows only one `:' modifier on each `$' expansion."
414.PP
415The following substitutions may not be modified with `:' modifiers.
416.HP 5
417$?name
418.br
419.ns
420.HP 5
421${?name}
422.br
423Substitutes the string `1' if name is set, `0' if it is not.
424.HP 5
425$?0
426.br
427Substitutes `1' if the current input filename is know, `0' if it is not.
428.HP 5
429$$
430.br
431Substitute the (decimal) process number of the (parent) shell.
432.PP
433.B "Command and filename substitution"
434.PP
435The remaining substitutions, command and filename substitution,
436are applied selectively to the arguments of builtin commands.
437This means that portions of expressions which are not evaluated are
438not subjected to these expansions.
439For commands which are not internal to the shell, the command
440name is substituted separately from the argument list.
441This occurs very late,
442after input-output redirection is performed, and in a child
443of the main shell.
444.PP
445.B "Command substitution"
446.PP
447Command substitution is indicated by a command enclosed in `\*(ga'.
448The output from such a command is normally broken into separate words
449at blanks, tabs and newlines, with null words being discarded,
450this text then replacing the original string.
451Within `"'s, only newlines force new words; blanks and tabs are preserved.
452.PP
453In any case, the single final newline does not force a new word.
454Note that it is thus possible for a command substitution to yield
455only part of a word, even if the command outputs a complete line.
456.PP
457.B "Filename substitution"
458.PP
459If a word contains any of the characters `*', `?', `[' or `{'
460or begins with the character `~', then that word is a candidate for
461filename substitution, also known as `globbing'.
462This word is then regarded as a pattern, and replaced with an alphabetically
463sorted list of file names which match the pattern.
464In a list of words specifying filename substitution it is an error for
465no pattern to match an existing file name, but it is not required
466for each pattern to match.
467Only the metacharacters `*', `?' and `[' imply pattern matching,
468the characters `~' and `{' being more akin to abbreviations.
469.PP
470In matching filenames, the character `.' at the beginning of a filename
471or immediately following a `/', as well as the character `/' must
472be matched explicitly.
473The character `*' matches any string of characters, including the null
474string.
475The character `?' matches any single character.
476The sequence `[...]' matches any one of the characters enclosed.
477Within `[...]',
478a pair of characters separated by `\-' matches any character lexically between
479the two.
480.PP
481The character `~' at the beginning of a filename is used to refer to home
482directories.
483Standing alone, i.e. `~' it expands to the invokers home directory as reflected
484in the value of the variable
485.I home.
486When followed by a name consisting of letters, digits and `\-' characters
487the shell searches for a user with that name and substitutes their
488home directory; thus `~ken' might expand to `/usr/ken' and `~ken/chmach'
489to `/usr/ken/chmach'.
490If the character `~' is followed by a character other than a letter or `/'
491or appears not at the beginning of a word,
492it is left undisturbed.
493.PP
494The metanotation `a{b,c,d}e' is a shorthand for `abe ace ade'.
495Left to right order is preserved, with results of matches being sorted
496separately at a low level to preserve this order.
497This construct may be nested.
498Thus `~source/s1/{oldls,ls}.c' expands to
499`/usr/source/s1/oldls.c /usr/source/s1/ls.c'
500whether or not these files exist without any chance of error
501if the home directory for `source' is `/usr/source'.
502Similarly `../{memo,*box}' might expand to `../memo ../box ../mbox'.
503(Note that `memo' was not sorted with the results of matching `*box'.)
504As a special case `{', `}' and `{}' are passed undisturbed.
505.PP
506.B Input/output
507.PP
508The standard input and standard output of a command may be redirected
509with the following syntax:
510.HP 5
511< name
512.br
513Open file
514.I name
515(which is first variable, command and filename expanded) as the standard
516input.
517.HP 5
518<< word
519.br
520Read the shell input up to a line which is identical to
521.I word.
522.I Word
523is not subjected to variable, filename or command substitution,
524and each input line is compared to
525.I word
526before any substitutions are done on this input line.
527Unless a quoting `\e', `"', `\*(aa' or `\*(ga' appears in
528.I word
529variable and command substitution is performed on the intervening lines,
530allowing `\e' to quote `$', `\e' and `\*(ga'.
531Commands which are substituted have all blanks, tabs, and newlines
532preserved, except for the final newline which is dropped.
533The resultant text is placed in an anonymous temporary file which
534is given to the command as standard input.
535.HP 5
536> name
537.br
538.ns
539.HP 5
540>! name
541.br
542.ns
543.HP 5
544>& name
545.br
546.ns
547.HP 5
548>&! name
549.br
550The file
551.I name
552is used as standard output.
553If the file does not exist then it is created;
554if the file exists, its is truncated, its previous contents being lost.
555.IP
556If the variable
557.I noclobber
558is set, then the file must not exist or be a character special file (e.g. a
559terminal or `/dev/null') or an error results.
560This helps prevent accidental destruction of files.
561In this case the `!' forms can be used and suppress this check.
562.IP
563The forms involving `&' route the diagnostic output into the specified
564file as well as the standard output.
565.I Name
566is expanded in the same way as `<' input filenames are.
567.HP 5
568>> name
569.br
570.ns
571.HP 5
572>>& name
573.br
574.ns
575.HP 5
576>>! name
577.br
578.ns
579.HP 5
580>>&! name
581.br
582Uses file
583.I name
584as standard output like `>' but places output at the end of the file.
585If the variable
586.I noclobber
587is set, then
588it is an error for the file not to exist unless
589one of the `!' forms is given.
590Otherwise similar to `>'.
591.PP
592If a command is run detached (followed by `&') then the default standard
593input for the command is the empty file `/dev/null'.
594Otherwise the command receives the environment in which the shell was
595invoked as modified by the input-output parameters and
596the presence of the command in a pipeline.
597Thus, unlike some previous shells, commands run from a file of shell commands
598have no access to the text of the commands by default; rather
599they receive the original standard input of the shell.
600The `<<' mechanism should be used to present inline data.
601This permits shell command scripts to function as components of pipelines
602and allows the shell to block read its input.
603.PP
604Diagnostic output may be directed through a pipe with the standard output.
605Simply use the form `|\|&' rather than just `|'.
606.PP
607.B Expressions
608.PP
609A number of the builtin commands (to be described subsequently)
610take expressions, in which the operators are similar to those of C, with
611the same precedence.
612These expressions appear in the
613.I @,
614.I exit,
615.I if,
616and
617.I while
618commands.
619The following operators are available:
620.DT
621.PP
622 |\|\|| && | \*(ua & == != <= >= < > << >> + \- * / % ! ~ ( )
623.PP
624Here the precedence increases to the right,
625`==' and `!=', `<=' `>=' `<' and `>', `<<' and `>>', `+' and `\-',
626`*' `/' and `%' being, in groups, at the same level.
627The `==' and `!=' operators compare their arguments as strings,
628all others operate on numbers.
629Strings which begin with `0' are considered octal numbers.
630Null or missing arguments are considered `0'.
631The result of all expressions are strings,
632which represent decimal numbers.
633It is important to note that no two components of an expression can appear
634in the same word; except when adjacent to components of expressions which
635are syntactically significant to the parser (`&' `|' `<' `>' `(' `)')
636they should be surrounded by spaces.
637.PP
638Also available in expressions as primitive operands are command executions
639enclosed in `{' and `}'
640and file enquiries of the form `\-\fIl\fR name' where
641.I l
642is one of:
643.PP
644.DT
645.nf
646 r read access
647 w write access
648 x execute access
649 e existence
650 o ownership
651 z zero size
652 f plain file
653 d directory
654.fi
655.PP
656The specified name is command and filename expanded and then tested
657to see if it has the specified relationship to the real user.
658If the file does not exist or is inaccessible then all enquiries return
659false, i.e. `0'.
660Command executions succeed, returning true, i.e. `1',
661if the command exits with status 0, otherwise they fail, returning
662false, i.e. `0'.
663If more detailed status information is required then the command
664should be executed outside of an expression and the variable
665.I status
666examined.
667.PP
668.B "Control flow"
669.PP
670The shell contains a number of commands which can be used to regulate the
671flow of control in command files (shell scripts) and
672(in limited but useful ways) from terminal input.
673These commands all operate by forcing the shell to reread or skip in its
674input and, due to the implementation, restrict the placement of some
675of the commands.
676.PP
677The
678.I foreach,
679.I switch,
680and
681.I while
682statements, as well as the
683.I if\-then\-else
684form of the
685.I if
686statement require that the major keywords appear in a single simple command
687on an input line as shown below.
688.PP
689If the shell's input is not seekable,
690the shell buffers up input whenever a loop is being read
691and performs seeks in this internal buffer to accomplish the rereading
692implied by the loop.
693(To the extent that this allows, backward goto's will succeed on
694non-seekable inputs.)
695.PP
696.B "Builtin commands"
697.PP
698Builtin commands are executed within the shell.
699If a builtin command occurs as any component of a pipeline
700except the last then it is executed in a subshell.
701.HP 5
702.B alias
703.br
704.ns
705.HP 5
706.BR alias " name"
707.br
708.ns
709.HP 5
710.BR alias " name wordlist"
711.br
712The first form prints all aliases.
713The second form prints the alias for name.
714The final form assigns the specified
715.I wordlist
716as the alias of
717.I name;
718.I wordlist
719is command and filename substituted.
720.I Name
721is not allowed to be
722.I alias
723or
724.I unalias
725.HP 5
726.B alloc
727.br
728Shows the amount of dynamic core in use, broken down into used and
729free core, and address of the last location in the heap.
730With an argument shows each used and free block on the internal dynamic
731memory chain indicating its address, size, and whether it is used or free.
732This is a debugging command and may not work in production versions of the
733shell; it requires a modified version of the system memory allocator.
734.HP 5
735.B break
736.br
737Causes execution to resume after the
738.I end
739of the nearest enclosing
740.I forall
741or
742.I while.
743The remaining commands on the current line are executed.
744Multi-level breaks are thus possible by writing them all on one line.
745.HP 5
746.B breaksw
747.br
748Causes a break from a
749.I switch,
750resuming after the
751.I endsw.
752.HP 5
753.BR case " label:"
754.br
755A label in a
756.I switch
757statement as discussed below.
758.HP 5
759.B cd
760.br
761.ns
762.HP 5
763.BR cd " name"
764.br
765.ns
766.HP 5
767.B chdir
768.br
769.ns
770.HP 5
771.BR chdir " name"
772.br
773Change the shells working directory to directory
774.I name.
775If no argument is given then change to the home directory of the user.
776.HP 5
777.B continue
778.br
779Continue execution of the nearest enclosing
780.I while
781or
782.I foreach.
783The rest of the commands on the current line are executed.
784.HP 5
785.B default:
786.br
787Labels the default case in a
788.I switch
789statement.
790The default should come after all
791.I case
792labels.
793.HP 5
794.BR echo " wordlist"
795.br
796The specified words are written to the shells standard output.
797A `\ec' causes the echo to complete without printing a newline,
798akin to the `\ec' in
799.IR nroff (1).
800A `\en' in wordlist causes a newline to be printed.
801Otherwise the words are echoed, separated by spaces.
802.HP 5
803.B else
804.br
805.ns
806.HP 5
807.B end
808.br
809.ns
810.HP 5
811.B endif
812.br
813.ns
814.HP 5
815.B endsw
816.br
817See the description of the
818.I foreach,
819.I if,
820.I switch,
821and
822.I while
823statements below.
824.HP 5
825.BR exec " command"
826.br
827The specified command is executed in place of the current shell.
828.HP 5
829.B exit
830.br
831.ns
832.HP 5
833.BR exit (expr)
834.br
835The shell exits either with the value of the
836.I status
837variable (first form) or with the value of the specified
838.I expr
839(second form).
840.HP 5
841.BR foreach " name (wordlist)"
842.br
843.ns
844.HP 5
845\ ...
846.br
847.ns
848.HP 5
849.B end
850.br
851The variable
852.I name
853is successively set to each member of
854.I wordlist
855and the sequence of commands between this command and the matching
856.I end
857are executed.
858(Both
859.I foreach
860and
861.I end
862must appear alone on separate lines.)
863.IP
864The builtin command
865.I continue
866may be used to continue the loop prematurely and the builtin
867command
868.I break
869to terminate it prematurely.
870When this command is read from the terminal, the loop is read up once
871prompting with `?' before any statements in the loop are executed.
872If you make a mistake typing in a loop at the terminal you can rub it out.
873.HP 5
874.BR glob " wordlist"
875.br
876Like
877.I echo
878but no `\e' escapes are recognized and words are delimited
879by null characters in the output.
880Useful for programs which wish to use the shell to filename expand a list
881of words.
882.HP 5
883.BR goto " word"
884.br
885The specified
886.I word
887is filename and command expanded to yield a string of the form `label'.
888The shell rewinds its input as much as possible
889and searches for a line of the form `label:'
890possibly preceded by blanks or tabs.
891Execution continues after the specified line.
892.HP 5
893.B history
894.br
895Displays the history event list.
896.HP 5
897.BR if " (expr) command"
898.br
899If the specified expression evaluates true, then the single
900.I command
901with arguments is executed.
902Variable substitution on
903.IR command ""
904happens early, at the same
905time it does for the rest of the
906.I if
907command.
908.I Command
909must be a simple command, not
910a pipeline, a command list, or a parenthesized command list.
911Input/output redirection occurs even if
912.I expr
913is false, when command is
914.B not
915executed (this is a bug).
916.HP 5
917.BR if " (expr) " "then"
918.br
919.ns
920.HP 5
921\ ...
922.br
923.ns
924.HP 5
925.BR else " " "if\fR (expr2) \fBthen"
926.br
927.ns
928.HP 5
929\ ...
930.br
931.ns
932.HP 5
933.B else
934.br
935.ns
936.HP 5
937\ ...
938.br
939.ns
940.HP 5
941.B endif
942.br
943If the specified
944.IR expr ""
945is true then the commands to the first
946.I else
947are executed; else if
948.IR expr2 ""
949is true then the commands to the
950second else are executed, etc.
951Any number of
952.I else-if
953pairs are possible; only one
954.I endif
955is needed.
956The
957.I else
958part is likewise optional.
959(The words
960.I else
961and
962.I endif
963must appear at the beginning of input lines;
964the
965.I if
966must appear alone on its input line or after an
967.I else.)
968.HP 5
969.B logout
970.br
971Terminate a login shell.
972Especially useful if
973.I ignoreeof
974is set.
975.HP 5
976.B nice
977.br
978.ns
979.HP 5
980.BR nice " \+number"
981.br
982.ns
983.HP 5
984.BR nice " command"
985.br
986.ns
987.HP 5
988.BR nice " \+number command"
989.br
990The first form sets the
991.I nice
992for this shell to 4.
993The second form sets the
994.I nice
995to the given number.
996The final two forms run command at priority 4 and
997.I number
998respectively.
999The super-user may specify negative niceness by using `nice \-number ...'.
1000Command is always executed in a sub-shell, and the restrictions
1001place on commands in simple
1002.I if
1003statements apply.
1004.HP 5
1005.B nohup
1006.br
1007.ns
1008.HP 5
1009.B "nohup command"
1010.br
1011The first form can be used in shell scripts to cause hangups to be
1012ignored for the remainder of the script.
1013The second form causes the specified command to be run with hangups
1014ignored. On the Computer Center systems at UC Berkeley, this also
1015.I submits
1016the process.
1017Unless the shell is running detached,
1018.I nohup
1019has no effect.
1020.HP 5
1021.B onintr
1022.br
1023.ns
1024.HP 5
1025.BR onintr " \-"
1026.br
1027.ns
1028.HP 5
1029.BR onintr " label"
1030.br
1031Control the action of the shell on interrupts.
1032The first form restores the default action of the shell on interrupts
1033which is to terminate shell scripts or to return to the terminal command
1034input level.
1035The second form `onintr \-' causes all interrupts to be ignored.
1036The final form causes the shell to execute a `goto label' when
1037an interrupt is received or a child process terminates because
1038it was interrupted.
1039.IP
1040In any case, if the shell is running detached and interrupts are
1041being ignored, all forms of
1042.I onintr
1043have no meaning and interrupts
1044continue to be ignored by the shell and all invoked commands.
1045.HP 5
1046.BR repeat " count command"
1047.br
1048The specified
1049.I command
1050which is subject to the same restrictions
1051as the
1052.I command
1053in the one line
1054.I if
1055statement above,
1056is executed
1057.I count
1058times.
1059I/O redirections occurs exactly once, even if
1060.I count
1061is 0.
1062.HP 5
1063.B set
1064.br
1065.ns
1066.HP 5
1067.BR set " name"
1068.br
1069.ns
1070.HP 5
1071.BR set " name=word"
1072.br
1073.ns
1074.HP 5
1075.BR set " name[index]=word"
1076.br
1077.ns
1078.HP 5
1079.BR set " name=(wordlist)"
1080.br
1081The first form of the command shows the value of all shell variables.
1082Variables which have other than a single word as value print as a parenthesized
1083word list.
1084The second form sets
1085.I name
1086to the null string.
1087The third form sets
1088.I name
1089to the single
1090.I word.
1091The fourth form sets
1092the
1093.I index'th
1094component of name to word;
1095this component must already exist.
1096The final form sets
1097.I name
1098to the list of words in
1099.I wordlist.
1100In all cases the value is command and filename expanded.
1101.IP
1102These arguments may be repeated to set multiple values in a single set command.
1103Note however, that variable expansion happens for all arguments before any
1104setting occurs.
1105.HP 5
1106.BR setenv " name value"
1107.br
1108(Version 7 systems only.)
1109Sets the value of environment variable
1110.I name
1111to be
1112.I value,
1113a single string.
1114Useful environment variables are `TERM' the type of your terminal and
1115`SHELL' the shell you are using.
1116.HP 5
1117.B shift
1118.br
1119.ns
1120.HP 5
1121.BR shift " variable"
1122.br
1123The members of
1124.I argv
1125are shifted to the left, discarding
1126.I argv[1].
1127It is an error for
1128.I argv
1129not to be set or to have less than one word as value.
1130The second form performs the same function on the specified variable.
1131.HP 5
1132.BR source " name"
1133.br
1134The shell reads commands from
1135.I name.
1136.I Source
1137commands may be nested; if they are nested too deeply the shell may
1138run out of file descriptors.
1139An error in a
1140.I source
1141at any level terminates all nested
1142.I source
1143commands.
1144Input during
1145.I source
1146commands is
1147.B never
1148placed on the history list.
1149.HP 5
1150.BR switch " (string)"
1151.br
1152.ns
1153.HP 5
1154.BR case " str1:"
1155.br
1156.ns
1157.HP 5
1158\ ...
1159.br
1160.ns
1161.HP 5
1162\
1163.B breaksw
1164.br
1165.ns
1166.HP 5
1167\&...
1168.br
1169.ns
1170.HP 5
1171.B default:
1172.br
1173.ns
1174.HP 5
1175\ ...
1176.br
1177.ns
1178.HP 5
1179\
1180.B breaksw
1181.br
1182.ns
1183.HP 5
1184.B endsw
1185.br
1186Each case label is successively matched, against the specified
1187.I string
1188which is first command and filename expanded.
1189The file metacharacters `*', `?' and `[...]' may be used in the case labels,
1190which are variable expanded.
1191If none of the labels match before a `default' label is found, then
1192the execution begins after the default label.
1193Each case label and the default label must appear at the beginning of a line.
1194The command
1195.I breaksw
1196causes execution to continue after the
1197.I endsw.
1198Otherwise control may fall through case labels and default labels as in C.
1199If no label matches and there is no default, execution continues after
1200the
1201.I endsw.
1202.HP 5
1203.B time
1204.br
1205.ns
1206.HP 5
1207.BR time " command"
1208.br
1209With no argument, a summary of time used by this shell and its children
1210is printed.
1211If arguments are given
1212the specified simple command is timed and a time summary
1213as described under the
1214.I time
1215variable is printed.
1216If necessary, an extra shell is created to print the time
1217statistic when the command completes.
1218.HP 5
1219.B umask
1220.br
1221.ns
1222.HP 5
1223.BR umask " value"
1224.br
1225The file creation mask is displayed (first form) or set to the specified
1226value (second form). The mask is given in octal. Common values for
1227the mask are 002 giving all access to the group and read and execute
1228access to others or 022 giving all access except no write access for
1229users in the group or others.
1230.HP 5
1231.BR unalias " pattern"
1232.br
1233All aliases whose names match the specified pattern are discarded.
1234Thus all aliases are removed by `unalias *'.
1235It is not an error for nothing to be
1236.I unaliased.
1237.HP 5
1238.BR unset " pattern"
1239.br
1240All variables whose names match the specified pattern are removed.
1241Thus all variables are removed by `unset *'; this has noticeably
1242distasteful side-effects.
1243It is not an error for nothing to be
1244.I unset.
1245.HP 5
1246.B wait
1247.br
1248All child processes are waited for.
1249It the shell is interactive, then an interrupt can disrupt the wait,
1250at which time the shell prints names and process numbers of all children
1251known to be outstanding.
1252.HP 5
1253.BR while " (expr)"
1254.br
1255.ns
1256.HP 5
1257\ ...
1258.br
1259.ns
1260.HP 5
1261.B end
1262.br
1263While the specified expression evaluates non-zero, the commands between
1264the
1265.I while
1266and the matching end are evaluated.
1267.I Break
1268and
1269.I continue
1270may be used to terminate or continue the loop prematurely.
1271(The
1272.I while
1273and
1274.I end
1275must appear alone on their input lines.)
1276Prompting occurs here the first time through the loop as for the
1277.I foreach
1278statement if the input is a terminal.
1279.HP 5
1280.B "@"
1281.br
1282.ns
1283.HP 5
1284.BR "@" " name = expr"
1285.br
1286.ns
1287.HP 5
1288.BR "@" " name[index] = expr"
1289.br
1290The first form prints the values of all the shell variables.
1291The second form sets the specified
1292.I name
1293to the value of
1294.I expr.
1295If the expression contains `<', `>', `&' or `|' then at least
1296this part of the expression must be placed within `(' `)'.
1297The third form assigns the value of
1298.I expr
1299to the
1300.I index'th
1301argument of
1302.I name.
1303Both
1304.I name
1305and its
1306.I index'th
1307component must already exist.
1308.IP
1309The operators `*=', `+=', etc are available as in C.
1310The space separating the name from the assignment operator is optional.
1311Spaces are, however, mandatory in separating components of
1312.I expr
1313which would otherwise be single words.
1314.IP
1315Special postfix `++' and `\-\-' operators increment and decrement
1316.I name
1317respectively, i.e. `@ i++'.
1318.PP
1319.B "Pre-defined variables"
1320.PP
1321The following variables have special meaning to the shell.
1322Of these,
1323.I argv,
1324.I child,
1325.I home,
1326.I path,
1327.I prompt,
1328.I shell
1329and
1330.I status
1331are always set by the shell.
1332Except for
1333.I child
1334and
1335.I status
1336this setting occurs only at initialization;
1337these variables will not then be modified unless this is done
1338explicitly by the user.
1339.TP 15
1340.B argv
1341\c
1342Set to the arguments to the shell, it is from this variable that
1343positional parameters are substituted, i.e. `$1' is replaced by
1344`$argv[1]', etc.
1345.TP 15
1346.B child
1347The process number printed when the last command was forked with `&'.
1348This variable is
1349.I unset
1350when this process terminates.
1351.TP 15
1352.B echo
1353\c
1354Set when the
1355.B \-x
1356command line option is given.
1357Causes each command and its arguments
1358to be echoed just before it is executed.
1359For non-builtin commands all expansions occur before echoing.
1360Builtin commands are echoed before command and filename substitution,
1361since these substitutions are then done selectively.
1362.TP 15
1363.B history
1364\c
1365Can be given a numeric value to control the size of the history list.
1366Any command which has been referenced in this many events will not be
1367discarded.
1368Too large values of
1369.I history
1370may run the shell out of memory.
1371The last executed command is always saved on the history list.
1372.TP 15
1373.B home
1374\c
1375The home directory of the invoker, initialized from the environment.
1376The filename expansion of `\fB~\fR' refers to this variable.
1377.TP 15
1378.B ignoreeof
1379\c
1380If set the shell ignores
1381end-of-file from input devices which are terminals.
1382This prevents shells from accidentally being killed by control-D's.
1383.TP 15
1384.B mail
1385\c
1386The files where the shell checks for mail.
1387This is done after each command completion which will result in a prompt,
1388if a specified interval has elapsed.
1389The shell says `You have new mail.'
1390if the file exists with an access time not greater than its modify time.
1391.IP
1392If the first word of the value of
1393.I mail
1394is numeric it specifies a different mail checking interval, in seconds,
1395than the default, which is 10 minutes.
1396.IP
1397If multiple mail files are specified, then the shell says
1398`New mail in
1399.IR name '
1400when there is mail in the file
1401.I name.
1402.TP 15
1403.B noclobber
1404\c
1405As described in the section on
1406.I Input/output,
1407restrictions are placed on output redirection to insure that
1408files are not accidentally destroyed, and that `>>' redirections
1409refer to existing files.
1410.TP 15
1411.B noglob
1412\c
1413If set, filename expansion is inhibited.
1414This is most useful in shell scripts which are not dealing with filenames,
1415or after a list of filenames has been obtained and further expansions
1416are not desirable.
1417.TP 15
1418.B nonomatch
1419\c
1420If set, it is not an error for a filename expansion to not match any
1421existing files; rather the primitive pattern is returned.
1422It is still an error for the primitive pattern to be malformed, i.e.
1423`echo [' still gives an error.
1424.TP 15
1425.B path
1426\c
1427Each word of the path variable specifies a directory in which
1428commands are to be sought for execution.
1429A null word specifies the current directory.
1430If there is no
1431.I path
1432variable then only full path names will execute.
1433The usual search path is `.', `/bin' and `/usr/bin', but this
1434may vary from system to system.
1435For the super-user the default search path is `/etc', `/bin' and `/usr/bin'.
1436.TP 15
1437.B prompt
1438\c
1439The string which is printed before each command is read from
1440an interactive terminal input.
1441If a `!' appears in the string it will be replaced by the current event number
1442unless a preceding `\e' is given.
1443Default is `% ', or `# ' for the super-user.
1444.TP 15
1445.B shell
1446\c
1447The file in which the shell resides.
1448This is used in forking shells to interpret files which have execute
1449bits set, but which are not executable by the system.
1450(See the description of
1451.I "Non-builtin Command Execution"
1452below.)
1453Initialized to the (system-dependent) home of the shell.
1454.TP 15
1455.B status
1456\c
1457The status returned by the last command.
1458If it terminated abnormally, then 0200 is added to the status.
1459Builtin commands which fail return exit status `1',
1460all other builtin commands set status `0'.
1461.TP 15
1462.B time
1463\c
1464Controls automatic timing of commands.
1465If set, then any command which takes more than this many cpu seconds
1466will cause a line giving user, system, and real times and a utilization
1467percentage which is the ratio of user plus system times to real time
1468to be printed when it terminates.
1469.TP 15
1470.B verbose
1471\c
1472Set by the
1473.B \-v
1474command line option, causes the words of each command to be printed
1475after history substitution.
1476.PP
1477.B "Non-builtin command execution"
1478.PP
1479When a command to be executed is found to not be a builtin command
1480the shell attempts to execute the command via
1481.IR exec (2).
1482Each word in the variable
1483.I path
1484names a directory which
1485the shell concatenates with the given command name to form a path name
1486of a file which it then attempts to execute.
1487Parenthesized commands are always executed in a subshell.
1488Thus `(cd ; pwd) ; pwd' prints the
1489.I home
1490directory; leaving you where you were (printing this after the home directory),
1491while `cd ; pwd' leaves you in the
1492.I home
1493directory.
1494Parenthesized commands are most often used to prevent
1495.I chdir
1496from affecting the current shell.
1497.PP
1498If the file has execute permissions but is not an
1499executable binary to the system, then it is assumed to be a
1500file containing shell commands an a new shell is spawned to read it.
1501.PP
1502If there is an
1503.I alias
1504for
1505.I shell
1506then the words of the alias will be prepended to the argument list to form
1507the shell command.
1508The first word of the
1509.I alias
1510should be the full path name of the shell
1511(e.g. `$shell').
1512Note that this is a special, late occurring, case of
1513.I alias
1514substitution,
1515and only allows words to be prepended to the argument list without modification.
1516.PP
1517.B "Argument list processing"
1518.PP
1519If argument 0 to the shell is `\-' then this
1520is a login shell.
1521The flag arguments are interpreted as follows:
1522.TP 5
1523.B \-c
1524\c
1525Commands are read from the (single) following argument which must
1526be present.
1527Any remaining arguments are placed in
1528.I argv.
1529.TP 5
1530.B \-e
1531\c
1532The shell exits if any invoked command terminates abnormally
1533or yields a non-zero exit status.
1534.TP 5
1535.B \-f
1536\c
1537The shell will start faster, because it will neither search for nor
1538execute commands from the file
1539`\&.cshrc' in the invokers home directory.
1540.TP 5
1541.B \-i
1542\c
1543The shell is interactive and prompts for its top-level input,
1544even if it appears to not be a terminal.
1545Shells are interactive without this option if their inputs
1546and outputs are terminals.
1547.TP 5
1548.B \-n
1549\c
1550Commands are parsed, but not executed.
1551This may aid in syntactic checking of shell scripts.
1552.TP 5
1553.B \-s
1554\c
1555Command input is taken from the standard input.
1556.TP 5
1557.B \-t
1558\c
1559A single line of input is read and executed.
1560A `\e' may be used to escape the newline at the end of this
1561line and continue onto another line.
1562.TP 5
1563.B \-v
1564\c
1565Causes the
1566.I verbose
1567variable to be set, with the effect
1568that command input is echoed after history substitution.
1569.TP 5
1570.B \-x
1571\c
1572Causes the
1573.I echo
1574variable to be set, so that commands are echoed immediately before execution.
1575.TP 5
1576.B \-V
1577\c
1578Causes the
1579.I verbose
1580variable to be set even before `\&.cshrc' is executed.
1581.TP 5
1582.B \-X
1583\c
1584Is to
1585.B \-x
1586as
1587.B \-V
1588is to
1589.B \-v.
1590.PP
1591After processing of flag arguments if arguments remain but none of the
1592.B \-c,
1593.B \-i,
1594.B \-s,
1595or
1596.B \-t
1597options was given the first argument is taken as the name of a file of
1598commands to be executed.
1599The shell opens this file, and saves its name for possible resubstitution
1600by `$0'.
1601Since many systems use either the standard version 6 or version 7 shells
1602whose shell scripts are not compatible with this shell, the shell will
1603execute such a `standard' shell if the first character of a script
1604is not a `#', i.e. if the script does not start with a comment.
1605Remaining arguments initialize the variable
1606.I argv.
1607.PP
1608.B "Signal handling"
1609.PP
1610The shell normally ignores
1611.I quit
1612signals.
1613The
1614.I interrupt
1615and
1616.I quit
1617signals are ignored for an invoked command
1618if the command is followed by `&';
1619otherwise the signals have the values which the shell inherited
1620from its parent.
1621The shells handling of interrupts can be controlled by
1622.I onintr.
1623Login shells catch the
1624.I terminate
1625signal; otherwise this signal is passed on to children from the state in the
1626shell's parent.
1627In no case are interrupts allowed when a login shell is reading the file
1628`\&.logout'.
1629.SH AUTHOR
1630William Joy
1631.SH FILES
1632.DT
1633.nf
1634~/.cshrc Read at beginning of execution by each shell.
1635~/.login Read by login shell, after `.cshrc' at login.
1636~/.logout Read by login shell, at logout.
1637/bin/sh Standard shell, for shell scripts not starting with a `#'.
1638/tmp/sh* Temporary file for `<<'.
1639/dev/null Source of empty file.
1640/etc/passwd Source of home directories for `~name'.
1641.fi
1642.SH LIMITATIONS
1643Words can be no longer that 512 characters.
1644The number of characters in an argument varies from system to system.
1645Early version 6 systems typically have 512 character limits
1646while later version 6 and version 7 systems have 5120 character limits.
1647The number of arguments to a command which involves filename expansion
1648is limited to 1/6'th the number of characters allowed in an argument list.
1649Also command substitutions may substitute no more characters than are
1650allowed in an argument list.
1651.PP
1652To detect looping, the shell restricts the number of
1653.I alias
1654substititutions on a single line to 20.
1655.SH "SEE ALSO"
1656access(2), exec(2), fork(2), pipe(2), signal(2), umask(2), wait(2),
1657a.out(5), environ(5),
1658`An introduction to the C shell'
1659.SH BUGS
1660Control structure should be parsed rather than being recognized as built-in
1661commands. This would allow control commands to be placed anywhere,
1662to be combined with `|', and to be used with `&' and `;' metasyntax.
1663.PP
1664Commands within loops, prompted for by `?', are not placed in the
1665.I history
1666list.
1667.PP
1668It should be possible to use the `:' modifiers on the output of command
1669substitutions.
1670All and more than one `:' modifier should be allowed on `$' substitutions.
1671.PP
1672Some commands should not touch
1673.I status
1674or it may be so transient as to be almost useless.
1675Oring in 0200 to
1676.I status
1677on abnormal termination is a kludge.
1678.PP
1679In order to be able to recover from failing
1680.I exec
1681commands on version 6 systems, the new command inherits several
1682open files other than the normal standard input and output and diagnostic
1683output.
1684If the input and output are redirected and the new command does not
1685close these files, some files may be held open unnecessarily.