386BSD 0.1 development
[unix-history] / usr / src / bin / csh / csh.1
CommitLineData
6febb542
WJ
1.\" Copyright (c) 1980, 1990 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\" notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\" notice, this list of conditions and the following disclaimer in the
11.\" documentation and/or other materials provided with the distribution.
12.\" 3. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\" @(#)csh.1 6.17 (Berkeley) 6/7/91
33.\"
34.Dd June 7, 1991
35.Dt CSH 1
36.Os BSD 4
37.Sh NAME
38.Nm csh
39.Nd a shell (command interpreter) with C-like syntax
40.Sh SYNOPSIS
41.Nm csh
42.Op Fl bcefinstvVxX
43.Op arg ...
44.Sh DESCRIPTION
45The
46.Nm Csh
47is a command language interpreter
48incorporating a history mechanism (see
49.Nm History Substitutions ) ,
50job control facilities (see
51.Nm Jobs ) ,
52interactive file name
53and user name completion (see
54.Nm File Name Completion ) ,
55and a C-like syntax. It is used both as an interactive
56login shell and a shell script command processor.
57.Ss Argument list processing
58If the first argument (argument 0) to the shell is
59.Ql Fl
60then this
61is a login shell.
62The flag arguments are interpreted as follows:
63.Bl -tag -width 5n
64.It Fl b
65This flag forces a ``break'' from option processing, causing any further
66shell arguments to be treated as non-option arguments.
67The remaining arguments will not be interpreted as shell options.
68This may be used to pass options to a shell script without confusion
69or possible subterfuge.
70The shell will not run a set-user ID script without this option.
71.It Fl c
72Commands are read from the (single) following argument which must
73be present.
74Any remaining arguments are placed in
75.Ar argv .
76.It Fl e
77The shell exits if any invoked command terminates abnormally
78or yields a non-zero exit status.
79.It Fl f
80The shell will start faster, because it will neither search for nor
81execute commands from the file
82.Pa \&.cshrc
83in the invoker's home directory.
84.It Fl i
85The shell is interactive and prompts for its top-level input,
86even if it appears to not be a terminal.
87Shells are interactive without this option if their inputs
88and outputs are terminals.
89.It Fl n
90Commands are parsed, but not executed.
91This aids in syntactic checking of shell scripts.
92.It Fl s
93Command input is taken from the standard input.
94.It Fl t
95A single line of input is read and executed.
96A
97.Ql \e
98may be used to escape the newline at the end of this
99line and continue onto another line.
100.It Fl v
101Causes the
102.Ar verbose
103variable to be set, with the effect
104that command input is echoed after history substitution.
105.It Fl x
106Causes the
107.Ar echo
108variable to be set, so that commands are echoed immediately before execution.
109.It Fl V
110Causes the
111.Ar verbose
112variable to be set even before
113.Pa .cshrc
114is executed.
115.It Fl X
116Is to
117.Fl x
118as
119.Fl V
120is to
121.Fl v .
122.El
123.Pp
124After processing of flag arguments, if arguments remain but none of the
125.Fl c ,
126.Fl i ,
127.Fl s ,
128or
129.Fl t
130options were given, the first argument is taken as the name of a file of
131commands to be executed.
132The shell opens this file, and saves its name for possible resubstitution
133by `$0'.
134Since many systems use either the standard version 6 or version 7 shells
135whose shell scripts are not compatible with this shell, the shell will
136execute such a `standard' shell if the first character of a script
137is not a `#', i.e. if the script does not start with a comment.
138Remaining arguments initialize the variable
139.Ar argv .
140.Pp
141An instance of
142.Nm csh
143begins by executing commands from the file
144.Pa /etc/csh.cshrc
145and,
146if this is a login shell,
147.Pa \&/etc/csh.login .
148It then executes
149commands from
150.Pa \&.cshrc
151in the
152.Ar home
153directory of the invoker, and, if this is a login shell, the file
154.Pa \&.login
155in the same location.
156It is typical for users on crt's to put the command ``stty crt''
157in their
158.Pa \&.login
159file, and to also invoke
160.Xr tset 1
161there.
162.Pp
163In the normal case, the shell will begin reading commands from the
164terminal, prompting with `% '.
165Processing of arguments and the use of the shell to process files
166containing command scripts will be described later.
167.Pp
168The shell repeatedly performs the following actions:
169a line of command input is read and broken into
170.Ar words .
171This sequence of words is placed on the command history list and parsed.
172Finally each command in the current line is executed.
173.Pp
174When a login shell terminates it executes commands from the files
175.Pa .logout
176in the user's
177.Ar home
178directory and
179.Pa /etc/csh.logout .
180.Ss Lexical structure
181The shell splits input lines into words at blanks and tabs with the
182following exceptions.
183The characters
184`&' `\&|' `;' `<' `>' `(' `)'
185form separate words.
186If doubled in `&&', `\&|\&|', `<<' or `>>' these pairs form single words.
187These parser metacharacters may be made part of other words, or prevented their
188special meaning, by preceding them with `\e'.
189A newline preceded by a `\e' is equivalent to a blank.
190.Pp
191Strings enclosed in matched pairs of quotations,
192`\*(aa', `\*(ga' or `"',
193form parts of a word; metacharacters in these strings, including blanks
194and tabs, do not form separate words.
195These quotations have semantics to be described subsequently.
196Within pairs of `\'' or `"' characters a newline preceded by a `\e' gives
197a true newline character.
198.Pp
199When the shell's input is not a terminal,
200the character `#' introduces a comment which continues to the end of the
201input line.
202It is prevented this special meaning when preceded by `\e'
203and in quotations using `\`', `\'', and `"'.
204.Ss Commands
205A simple command is a sequence of words, the first of which
206specifies the command to be executed.
207A simple command or
208a sequence of simple commands separated by `\&|' characters
209forms a pipeline.
210The output of each command in a pipeline is connected to the input of the next.
211Sequences of pipelines may be separated by `;', and are then executed
212sequentially.
213A sequence of pipelines may be executed without immediately
214waiting for it to terminate by following it with an `&'.
215.Pp
216Any of the above may be placed in `(' `)' to form a simple command (which
217may be a component of a pipeline, etc.)
218It is also possible to separate pipelines with `\&|\&|' or `&&' indicating,
219as in the C language,
220that the second is to be executed only if the first fails or succeeds
221respectively. (See
222.Em Expressions . )
223.Ss Jobs
224The shell associates a
225.Ar job
226with each pipeline. It keeps
227a table of current jobs, printed by the
228.Ar jobs
229command, and assigns them small integer numbers. When
230a job is started asynchronously with `&', the shell prints a line which looks
231like:
232.Bd -filled -offset indent
233.Op 1
2341234
235.Ed
236.Pp
237indicating that the job which was started asynchronously was job number
2381 and had one (top-level) process, whose process id was 1234.
239.Pp
240If you are running a job and wish to do something else you may hit the key
241.Nm ^Z
242(control-Z) which sends a STOP signal to the current job.
243The shell will then normally indicate that the job has been `Stopped',
244and print another prompt. You can then manipulate the state of this job,
245putting it in the
246.Em background
247with the
248.Ar bg
249command, or run some other
250commands and then eventually bring the job back into the foreground with
251the
252.Em foreground
253command
254.Ar fg .
255A
256.Nm ^Z
257takes effect immediately and
258is like an interrupt in that pending output and unread input are discarded
259when it is typed. There is another special key
260.Nm ^Y
261which does
262not generate a STOP signal until a program attempts to
263.Xr read 2
264it.
265This can usefully be typed ahead when you have prepared some commands
266for a job which you wish to stop after it has read them.
267.Pp
268A job being run in the background will stop if it tries to read
269from the terminal. Background jobs are normally allowed to produce output,
270but this can be disabled by giving the command ``stty tostop''.
271If you set this
272tty option, then background jobs will stop when they try to produce
273output like they do when they try to read input.
274.Pp
275There are several ways to refer to jobs in the shell. The character
276`%' introduces a job name. If you wish to refer to job number 1, you can
277name it as `%1'. Just naming a job brings it to the foreground; thus
278`%1' is a synonym for `fg %1', bringing job 1 back into the foreground.
279Similarly saying `%1 &' resumes job 1 in the background.
280Jobs can also be named by prefixes of the string typed in to start them,
281if these prefixes are unambiguous, thus `%ex' would normally restart
282a suspended
283.Xr ex 1
284job, if there were only one suspended job whose name began with
285the string `ex'. It is also possible to say `%?string'
286which specifies a job whose text contains
287.Ar string ,
288if there is only one such job.
289.Pp
290The shell maintains a notion of the current and previous jobs.
291In output pertaining to jobs, the current job is marked with a `+'
292and the previous job with a `\-'. The abbreviation `%+' refers
293to the current job and `%\-' refers to the previous job. For close
294analogy with the syntax of the
295.Ar history
296mechanism (described below),
297`%%' is also a synonym for the current job.
298.Pp
299The job control mechanism requires that the
300.Xr stty 1
301option
302.Ic new
303be set. It is an artifact from a
304.Em new
305implementation
306of the
307tty driver which allows generation of interrupt characters from
308the keyboard to tell jobs to stop. See stty(1) for details
309on setting options in the new tty driver.
310.Ss Status reporting
311This shell learns immediately whenever a process changes state.
312It normally informs you whenever a job becomes blocked so that
313no further progress is possible, but only just before it prints
314a prompt. This is done so that it does not otherwise disturb your work.
315If, however, you set the shell variable
316.Ar notify ,
317the shell will notify you immediately of changes of status in background
318jobs.
319There is also a shell command
320.Ar notify
321which marks a single process so that its status changes will be immediately
322reported. By default
323.Ar notify
324marks the current process;
325simply say `notify' after starting a background job to mark it.
326.Pp
327When you try to leave the shell while jobs are stopped, you will
328be warned that `You have stopped jobs.' You may use the
329.Ar jobs
330command to see what they are. If you do this or immediately try to
331exit again, the shell will not warn you a second time, and the suspended
332jobs will be terminated.
333.Ss File Name Completion
334When the file name completion feature is enabled by setting
335the shell variable
336.Ar filec
337(see
338.Ic set ) ,
339.Nm csh
340will
341interactively complete file names and user names from unique
342prefixes, when they are input from the terminal followed by
343the escape character (the escape key, or control-[)
344For example,
345if the current directory looks like
346.Bd -literal -offset indent
347DSC.OLD bin cmd lib xmpl.c
348DSC.NEW chaosnet cmtest mail xmpl.o
349bench class dev mbox xmpl.out
350.Ed
351.Pp
352and the input is
353.Pp
354.Dl % vi ch<escape>
355.Pp
356.Nm csh
357will complete the prefix ``ch''
358to the only matching file name ``chaosnet'', changing the input
359line to
360.Pp
361.Dl % vi chaosnet
362.Pp
363However, given
364.Pp
365.Dl % vi D<escape>
366.Pp
367.Nm csh
368will only expand the input to
369.Pp
370.Dl % vi DSC.
371.Pp
372and will sound the terminal bell to indicate that the expansion is
373incomplete, since there are two file names matching the prefix ``D''.
374.Pp
375If a partial file name is followed by the end-of-file character
376(usually control-D), then, instead of completing the name,
377.Nm csh
378will list all file names matching the prefix. For example,
379the input
380.Pp
381.Dl % vi D<control-D>
382.Pp
383causes all files beginning with ``D'' to be listed:
384.Pp
385.Dl DSC.NEW DSC.OLD
386.Pp
387while the input line remains unchanged.
388.Pp
389The same system of escape and end-of-file can also be used to
390expand partial user names, if the word to be completed
391(or listed) begins with the character ``~''. For example,
392typing
393.Pp
394.Dl cd ~ro<escape>
395.Pp
396may produce the expansion
397.Pp
398.Dl cd ~root
399.Pp
400The use of the terminal bell to signal errors or multiple matches
401can be inhibited by setting the variable
402.Ar nobeep .
403.Pp
404Normally, all files in the particular directory are candidates
405for name completion. Files with certain suffixes can be excluded
406from consideration by setting the variable
407.Ar fignore
408to the
409list of suffixes to be ignored. Thus, if
410.Ar fignore
411is set by
412the command
413.Pp
414.Dl % set fignore = (.o .out)
415.Pp
416then typing
417.Pp
418.Dl % vi x<escape>
419.Pp
420would result in the completion to
421.Pp
422.Dl % vi xmpl.c
423.Pp
424ignoring the files "xmpl.o" and "xmpl.out".
425However, if the only completion possible requires not ignoring these
426suffixes, then they are not ignored. In addition,
427.Ar fignore
428does not affect the listing of file names by control-D. All files
429are listed regardless of their suffixes.
430.Ss Substitutions
431We now describe the various transformations the shell performs on the
432input in the order in which they occur.
433.Ss History substitutions
434History substitutions place words from previous command input as portions
435of new commands, making it easy to repeat commands, repeat arguments
436of a previous command in the current command, or fix spelling mistakes
437in the previous command with little typing and a high degree of confidence.
438History substitutions begin with the character `!' and may begin
439.Ar anywhere
440in the input stream (with the proviso that they
441.Nm "do not"
442nest.)
443This `!' may be preceded by an `\e' to prevent its special meaning; for
444convenience, a `!' is passed unchanged when it is followed by a blank,
445tab, newline, `=' or `('.
446(History substitutions also occur when an input line begins with `\*(ua'.
447This special abbreviation will be described later.)
448Any input line which contains history substitution is echoed on the terminal
449before it is executed as it could have been typed without history substitution.
450.Pp
451Commands input from the terminal which consist of one or more words
452are saved on the history list.
453The history substitutions reintroduce sequences of words from these
454saved commands into the input stream.
455The size of which is controlled by the
456.Ar history
457variable; the previous command is always retained, regardless of its value.
458Commands are numbered sequentially from 1.
459.Pp
460For definiteness, consider the following output from the
461.Ar history
462command:
463.Bd -literal -offset indent
464\09 write michael
46510 ex write.c
46611 cat oldwrite.c
46712 diff *write.c
468.Ed
469.Pp
470The commands are shown with their event numbers.
471It is not usually necessary to use event numbers, but the current event
472number can be made part of the
473.Ar prompt
474by placing an `!' in the prompt string.
475.Pp
476With the current event 13 we can refer to previous events by event
477number `!11', relatively as in `!\-2' (referring to the same event),
478by a prefix of a command word
479as in `!d' for event 12 or `!wri' for event 9, or by a string contained in
480a word in the command as in `!?mic?' also referring to event 9.
481These forms, without further modification, simply reintroduce the words
482of the specified events, each separated by a single blank.
483As a special case `!!' refers to the previous command; thus `!!'
484alone is essentially a
485.Ar redo .
486.Pp
487To select words from an event we can follow the event specification by
488a `:' and a designator for the desired words.
489The words of an input line are numbered from 0,
490the first (usually command) word being 0, the second word (first argument)
491being 1, etc.
492The basic word designators are:
493.Pp
494.Bl -tag -width Ds -compact -offset indent
495.It \&0
496first (command) word
497.It Ar n
498.Ar n Ns \'th
499argument
500.It \*(ua
501first argument, i.e. `1'
502.It $
503last argument
504.It %
505word matched by (immediately preceding)
506.No \&? Ns Ar s Ns \?
507search
508.It Ar \&x\-y
509range of words
510.It Ar \&\-y
511abbreviates
512.Ar `\&0\-y\'
513.It *
514abbreviates `\*(ua\-$', or nothing if only 1 word in event
515.It Ar x*
516abbreviates
517.Ar `x\-$\'
518.It Ar x\-
519like
520.Ar `x*\'
521but omitting word `$'
522.El
523.Pp
524The `:' separating the event specification from the word designator
525can be omitted if the argument selector begins with a `\*(ua', `$', `*'
526`\-' or `%'.
527After the optional word designator can be
528placed a sequence of modifiers, each preceded by a `:'.
529The following modifiers are defined:
530.Pp
531.Bl -tag -width Ds -compact -offset indent
532.It h
533Remove a trailing pathname component, leaving the head.
534.It r
535Remove a trailing `.xxx' component, leaving the root name.
536.It e
537Remove all but the extension `.xxx' part.
538.It s Ns Ar /l/r/
539Substitute
540.Ar l
541for
542.Ar r
543.It t
544Remove all leading pathname components, leaving the tail.
545.It \&&
546Repeat the previous substitution.
547.It g
548Apply the change globally, prefixing the above, e.g. `g&'.
549.It p
550Print the new command line but do not execute it.
551.It q
552Quote the substituted words, preventing further substitutions.
553.It x
554Like q, but break into words at blanks, tabs and newlines.
555.El
556.Pp
557Unless preceded by a `g' the modification is applied only to the first
558modifiable word. With substitutions, it is an error for no word to be
559applicable.
560.Pp
561The left hand side of substitutions are not regular expressions in the sense
562of the editors, but rather strings.
563Any character may be used as the delimiter in place of `/';
564a `\e' quotes the delimiter into the
565.Ar l " "
566and
567.Ar r " "
568strings.
569The character `&' in the right hand side is replaced by the text from
570the left.
571A `\e' quotes `&' also.
572A null
573.Ar l
574(" ")
575uses the previous string either from a
576.Ar l
577or from a
578contextual scan string
579.Ar s
580in `!?
581.Ar s
582\?'.
583The trailing delimiter in the substitution may be omitted if a newline
584follows immediately as may the trailing `?' in a contextual scan.
585.Pp
586A history reference may be given without an event specification, e.g. `!$'.
587In this case the reference is to the previous command unless a previous
588history reference occurred on the same line in which case this form repeats
589the previous reference.
590Thus `!?foo?\*(ua !$' gives the first and last arguments
591from the command matching `?foo?'.
592.Pp
593A special abbreviation of a history reference occurs when the first
594non-blank character of an input line is a `\*(ua'.
595This is equivalent to `!:s\*(ua' providing a convenient shorthand for substitutions
596on the text of the previous line.
597Thus `\*(ualb\*(ualib' fixes the spelling of
598`lib'
599in the previous command.
600Finally, a history substitution may be surrounded with `{' and `}'
601if necessary to insulate it from the characters which follow.
602Thus, after `ls \-ld ~paul' we might do `!{l}a' to do `ls \-ld ~paula',
603while `!la' would look for a command starting `la'.
604.Pp
605.Ss Quotations with \' and \&"
606The quotation of strings by `\'' and `"' can be used
607to prevent all or some of the remaining substitutions.
608Strings enclosed in `\'' are prevented any further interpretation.
609Strings enclosed in `"' may be expanded as described below.
610.Pp
611In both cases the resulting text becomes (all or part of) a single word;
612only in one special case (see
613.Em Command Substitition
614below) does a `"' quoted string yield parts of more than one word;
615`\'' quoted strings never do.
616.Ss Alias substitution
617The shell maintains a list of aliases which can be established, displayed
618and modified by the
619.Ar alias
620and
621.Ar unalias
622commands.
623After a command line is scanned, it is parsed into distinct commands and
624the first word of each command, left-to-right, is checked to see if it
625has an alias.
626If it does, then the text which is the alias for that command is reread
627with the history mechanism available
628as though that command were the previous input line.
629The resulting words replace the
630command and argument list.
631If no reference is made to the history list, then the argument list is
632left unchanged.
633.Pp
634Thus if the alias for `ls' is `ls \-l' the command `ls /usr' would map to
635`ls \-l /usr', the argument list here being undisturbed.
636Similarly if the alias for `lookup' was `grep !\*(ua /etc/passwd' then
637`lookup bill' would map to `grep bill /etc/passwd'.
638.Pp
639If an alias is found, the word transformation of the input text
640is performed and the aliasing process begins again on the reformed input line.
641Looping is prevented if the first word of the new text is the same as the old
642by flagging it to prevent further aliasing.
643Other loops are detected and cause an error.
644.Pp
645Note that the mechanism allows aliases to introduce parser metasyntax.
646Thus we can `alias print \'pr \e!* \&| lpr\'' to make a command which
647.Ar pr \'s
648its arguments to the line printer.
649.Ss Variable substitution
650The shell maintains a set of variables, each of which has as value a list
651of zero or more words.
652Some of these variables are set by the shell or referred to by it.
653For instance, the
654.Ar argv
655variable is an image of the shell's argument list, and words of this
656variable's value are referred to in special ways.
657.Pp
658The values of variables may be displayed and changed by using the
659.Ar set
660and
661.Ar unset
662commands.
663Of the variables referred to by the shell a number are toggles;
664the shell does not care what their value is,
665only whether they are set or not.
666For instance, the
667.Ar verbose
668variable is a toggle which causes command input to be echoed.
669The setting of this variable results from the
670.Fl v
671command line option.
672.Pp
673Other operations treat variables numerically.
674The `@' command permits numeric calculations to be performed and the result
675assigned to a variable.
676Variable values are, however, always represented as (zero or more) strings.
677For the purposes of numeric operations, the null string is considered to be
678zero, and the second and subsequent words of multiword values are ignored.
679.Pp
680After the input line is aliased and parsed, and before each command
681is executed, variable substitution
682is performed keyed by `$' characters.
683This expansion can be prevented by preceding the `$' with a `\e' except
684within `"'s where it
685.Em always
686occurs, and within `\''s where it
687.Em never
688occurs.
689Strings quoted by `\*(ga' are interpreted later (see
690.Nm "Command substitution"
691below) so `$' substitution does not occur there until later, if at all.
692A `$' is passed unchanged if followed by a blank, tab, or end-of-line.
693.Pp
694Input/output redirections are recognized before variable expansion,
695and are variable expanded separately.
696Otherwise, the command name and entire argument list are expanded together.
697It is thus possible for the first (command) word to this point to generate
698more than one word, the first of which becomes the command name,
699and the rest of which become arguments.
700.Pp
701Unless enclosed in `"' or given the `:q' modifier the results of variable
702substitution may eventually be command and filename substituted.
703Within `"', a variable whose value consists of multiple words expands to a
704(portion of) a single word, with the words of the variables value
705separated by blanks.
706When the `:q' modifier is applied to a substitution
707the variable will expand to multiple words with each word separated
708by a blank and quoted to prevent later command or filename substitution.
709.Pp
710The following metasequences are provided for introducing variable values into
711the shell input.
712Except as noted, it is an error to reference a variable which is not set.
713.Pp
714.Bl -tag -width Ds -compact -offset indent
715.It $name
716.It ${name}
717Are replaced by the words of the value of variable
718.Ar name ,
719each separated by a blank.
720Braces insulate
721.Ar name
722from following characters which would otherwise be part of it.
723Shell variables have names consisting of up to 20 letters and digits
724starting with a letter. The underscore character is considered a letter.
725.br
726If
727.Ar name
728is not a shell variable, but is set in the environment, then
729that value is returned (but
730.Nm :
731modifiers and the other forms
732given below are not available in this case).
733.It $name Ns Op selector
734.It ${name Ns Op selector Ns }
735May be used to select only some of the words from the value of
736.Ar name .
737The selector is subjected to `$' substitution and may consist of a single
738number or two numbers separated by a `\-'.
739The first word of a variables value is numbered `1'.
740If the first number of a range is omitted it defaults to `1'.
741If the last member of a range is omitted it defaults to `$#name'.
742The selector `*' selects all words.
743It is not an error for a range to be empty if the second argument is omitted
744or in range.
745.It $#name
746.It ${#name}
747Gives the number of words in the variable.
748This is useful for later use in a
749`$argv[selector]'.
750.It $0
751Substitutes the name of the file from which command input is being read.
752An error occurs if the name is not known.
753.It $number
754.It ${number}
755Equivalent to
756`$argv[number]'.
757.It $*
758Equivalent to
759`$argv[*]'.
760The modifiers `:e', `:h', `:t', `:r', `:q' and `:x' may be applied to
761the substitutions above as may `:gh', `:gt' and `:gr'.
762If braces `{' '}' appear in the command form then the modifiers
763must appear within the braces.
764The current implementation allows only one `:' modifier on each `$' expansion.
765.El
766.Pp
767The following substitutions may not be modified with `:' modifiers.
768.Bl -tag -width Ds -compact -offset indent
769.It $?name
770.It ${?name}
771Substitutes the string `1' if name is set, `0' if it is not.
772.It $?0
773Substitutes `1' if the current input filename is known, `0' if it is not.
774.It $$
775Substitute the (decimal) process number of the (parent) shell.
776.It $<
777Substitutes a line from the standard
778input, with no further interpretation thereafter. It can be used
779to read from the keyboard in a shell script.
780.El
781.Ss Command and filename substitution
782The remaining substitutions, command and filename substitution,
783are applied selectively to the arguments of builtin commands.
784This means that portions of expressions which are not evaluated are
785not subjected to these expansions.
786For commands which are not internal to the shell, the command
787name is substituted separately from the argument list.
788This occurs very late,
789after input-output redirection is performed, and in a child
790of the main shell.
791.Ss Command substitution
792Command substitution is indicated by a command enclosed in `\*(ga'.
793The output from such a command is normally broken into separate words
794at blanks, tabs and newlines, with null words being discarded,
795this text then replacing the original string.
796Within `"'s, only newlines force new words; blanks and tabs are preserved.
797.Pp
798In any case, the single final newline does not force a new word.
799Note that it is thus possible for a command substitution to yield
800only part of a word, even if the command outputs a complete line.
801.Ss Filename substitution
802If a word contains any of the characters `*', `?', `[' or `{'
803or begins with the character `~', then that word is a candidate for
804filename substitution, also known as `globbing'.
805This word is then regarded as a pattern, and replaced with an alphabetically
806sorted list of file names which match the pattern.
807In a list of words specifying filename substitution it is an error for
808no pattern to match an existing file name, but it is not required
809for each pattern to match.
810Only the metacharacters `*', `?' and `[' imply pattern matching,
811the characters `~' and `{' being more akin to abbreviations.
812.Pp
813In matching filenames, the character `.' at the beginning of a filename
814or immediately following a `/', as well as the character `/' must
815be matched explicitly.
816The character `*' matches any string of characters, including the null
817string.
818The character `?' matches any single character.
819The sequence
820.Sq Op ...
821matches any one of the characters enclosed.
822Within
823.Sq Op ... ,
824a pair of characters separated by `\-' matches any character lexically between
825the two.
826.Pp
827The character `~' at the beginning of a filename is used to refer to home
828directories.
829Standing alone, i.e. `~' it expands to the invokers home directory as reflected
830in the value of the variable
831.Ar home .
832When followed by a name consisting of letters, digits and `\-' characters
833the shell searches for a user with that name and substitutes their
834home directory; thus `~ken' might expand to `/usr/ken' and `~ken/chmach'
835to `/usr/ken/chmach'.
836If the character `~' is followed by a character other than a letter or `/'
837or appears not at the beginning of a word,
838it is left undisturbed.
839.Pp
840The metanotation `a{b,c,d}e' is a shorthand for `abe ace ade'.
841Left to right order is preserved, with results of matches being sorted
842separately at a low level to preserve this order.
843This construct may be nested.
844Thus `~source/s1/{oldls,ls}.c' expands to
845`/usr/source/s1/oldls.c /usr/source/s1/ls.c'
846whether or not these files exist without any chance of error
847if the home directory for `source' is `/usr/source'.
848Similarly `../{memo,*box}' might expand to `../memo ../box ../mbox'.
849(Note that `memo' was not sorted with the results of matching `*box'.)
850As a special case `{', `}' and `{}' are passed undisturbed.
851.Ss Input/output
852The standard input and standard output of a command may be redirected
853with the following syntax:
854.Pp
855.Bl -tag -width Ds -compact -offset indent
856.It < name
857Open file
858.Ar name
859(which is first variable, command and filename expanded) as the standard
860input.
861.It << word
862Read the shell input up to a line which is identical to
863.Ar word .
864.Ar Word
865is not subjected to variable, filename or command substitution,
866and each input line is compared to
867.Ar word
868before any substitutions are done on this input line.
869Unless a quoting `\e', `"', `\*(aa' or `\*(ga' appears in
870.Ar word
871variable and command substitution is performed on the intervening lines,
872allowing `\e' to quote `$', `\e' and `\*(ga'.
873Commands which are substituted have all blanks, tabs, and newlines
874preserved, except for the final newline which is dropped.
875The resultant text is placed in an anonymous temporary file which
876is given to the command as standard input.
877.It > name
878.It >! name
879.It >& name
880.It >&! name
881The file
882.Ar name
883is used as standard output.
884If the file does not exist then it is created;
885if the file exists, its is truncated, its previous contents being lost.
886.Pp
887If the variable
888.Ar noclobber
889is set, then the file must not exist or be a character special file (e.g. a
890terminal or `/dev/null') or an error results.
891This helps prevent accidental destruction of files.
892In this case the `!' forms can be used and suppress this check.
893.Pp
894The forms involving `&' route the diagnostic output into the specified
895file as well as the standard output.
896.Ar Name
897is expanded in the same way as `<' input filenames are.
898.It >> name
899.It >>& name
900.It >>! name
901.It >>&! name
902Uses file
903.Ar name
904as standard output like `>' but places output at the end of the file.
905If the variable
906.Ar noclobber
907is set, then it is an error for the file not to exist unless
908one of the `!' forms is given.
909Otherwise similar to `>'.
910.El
911.Pp
912A command receives the environment in which the shell was
913invoked as modified by the input-output parameters and
914the presence of the command in a pipeline.
915Thus, unlike some previous shells, commands run from a file of shell commands
916have no access to the text of the commands by default; rather
917they receive the original standard input of the shell.
918The `<<' mechanism should be used to present inline data.
919This permits shell command scripts to function as components of pipelines
920and allows the shell to block read its input.
921Note that the default standard input for a command run detached is
922.Ar not
923modified to be the empty file
924.Pa /dev/null ;
925rather the standard input
926remains as the original standard input of the shell. If this is a terminal
927and if the process attempts to read from the terminal, then the process
928will block and the user will be notified (see
929.Sx Jobs
930above).
931.Pp
932Diagnostic output may be directed through a pipe with the standard output.
933Simply use the form `\&|&' rather than just `\&|'.
934.Ss Expressions
935A number of the builtin commands (to be described subsequently)
936take expressions, in which the operators are similar to those of C, with
937the same precedence.
938These expressions appear in the
939.Nm @,
940.Ar exit ,
941.Ar if ,
942and
943.Ar while
944commands.
945The following operators are available:
946.Bd -ragged -offset indent
947\&|\&| && \&| *(ua & == != =~ !~ <= >=
948< > << >> + \- * / % ! ~ ( )
949.Ed
950.Pp
951Here the precedence increases to the right,
952`==' `!=' `=~' and `!~', `<=' `>=' `<' and `>', `<<' and `>>', `+' and `\-',
953`*' `/' and `%' being, in groups, at the same level.
954The `==' `!=' `=~' and `!~' operators compare their arguments as strings;
955all others operate on numbers.
956The operators `=~' and `!~' are like `!=' and `==' except that the right
957hand side is a
958.Ar pattern
959(containing, e.g. `*'s, `?'s and instances of
960`[...]'
961against which the left hand operand is matched. This reduces the
962need for use of the
963.Ar switch
964statement in shell scripts when all that is really needed is pattern matching.
965.Pp
966Strings which begin with `0' are considered octal numbers.
967Null or missing arguments are considered `0'.
968The result of all expressions are strings,
969which represent decimal numbers.
970It is important to note that no two components of an expression can appear
971in the same word; except when adjacent to components of expressions which
972are syntactically significant to the parser (`&' `\&|' `<' `>' `(' `)')
973they should be surrounded by spaces.
974.Pp
975Also available in expressions as primitive operands are command executions
976enclosed in `{' and `}'
977and file enquiries of the form
978.Fl l
979.Ar name
980where
981.Ic l
982is one of:
983.Bd -ragged -offset indent
984r read access
985w write access
986x execute access
987e existence
988o ownership
989z zero size
990f plain file
991d directory
992.Ed
993.Pp
994The specified name is command and filename expanded and then tested
995to see if it has the specified relationship to the real user.
996If the file does not exist or is inaccessible then all enquiries return
997false, i.e. `0'.
998Command executions succeed, returning true, i.e. `1',
999if the command exits with status 0, otherwise they fail, returning
1000false, i.e. `0'.
1001If more detailed status information is required then the command
1002should be executed outside of an expression and the variable
1003.Ar status
1004examined.
1005.Ss Control flow
1006The shell contains a number of commands which can be used to regulate the
1007flow of control in command files (shell scripts) and
1008(in limited but useful ways) from terminal input.
1009These commands all operate by forcing the shell to reread or skip in its
1010input and, due to the implementation, restrict the placement of some
1011of the commands.
1012.Pp
1013The
1014.Ic foreach ,
1015.Ic switch ,
1016and
1017.Ic while
1018statements, as well as the
1019.Ic if\-then\-else
1020form of the
1021.Ic if
1022statement require that the major keywords appear in a single simple command
1023on an input line as shown below.
1024.Pp
1025If the shell's input is not seekable,
1026the shell buffers up input whenever a loop is being read
1027and performs seeks in this internal buffer to accomplish the rereading
1028implied by the loop.
1029(To the extent that this allows, backward goto's will succeed on
1030non-seekable inputs.)
1031.Ss Builtin commands
1032Builtin commands are executed within the shell.
1033If a builtin command occurs as any component of a pipeline
1034except the last then it is executed in a subshell.
1035.Pp
1036.Bl -tag -width Ds -compact -offset indent
1037.It Ic alias
1038.It Ic alias Ar name
1039.It Ic alias Ar name wordlist
1040The first form prints all aliases.
1041The second form prints the alias for name.
1042The final form assigns the specified
1043.Ar wordlist
1044as the alias of
1045.Ar name ;
1046.Ar wordlist
1047is command and filename substituted.
1048.Ar Name
1049is not allowed to be
1050.Ar alias
1051or
1052.Ar unalias .
1053.Pp
1054.It Ic alloc
1055Shows the amount of dynamic memory acquired, broken down into used and
1056free memory.
1057With an argument shows the number of free and used blocks in each size
1058category. The categories start at size 8 and double at each step.
1059This command's output may vary across system types, since
1060systems other than the VAX may use a different memory allocator.
1061.Pp
1062.It Ic bg
1063.It Ic bg \&% Ns Ar job ...
1064Puts the current or specified jobs into the background, continuing them
1065if they were stopped.
1066.Pp
1067.It Ic break
1068Causes execution to resume after the
1069.Ic end
1070of the nearest enclosing
1071.Ic foreach
1072or
1073.Ic while .
1074The remaining commands on the current line are executed.
1075Multi-level breaks are thus possible by writing them all on one line.
1076.Pp
1077.It Ic breaksw
1078Causes a break from a
1079.Ic switch ,
1080resuming after the
1081.Ic endsw .
1082.Pp
1083.It Ic case Ar label :
1084A label in a
1085.Ic switch
1086statement as discussed below.
1087.Pp
1088.It Ic cd
1089.It Ic cd Ar name
1090.It Ic chdir
1091.It Ic chdir Ar name
1092Change the shell's working directory to directory
1093.Ar name .
1094If no argument is given then change to the home directory of the user.
1095If
1096.Ar name
1097is not found as a subdirectory of the current directory (and does not begin
1098with `/', `./' or `../'), then each
1099component of the variable
1100.Ic cdpath
1101is checked to see if it has a subdirectory
1102.Ar name .
1103Finally, if all else fails but
1104.Ar name
1105is a shell variable whose value begins with `/', then this
1106is tried to see if it is a directory.
1107.Pp
1108.It Ic continue
1109Continue execution of the nearest enclosing
1110.Ic while
1111or
1112.Ic foreach .
1113The rest of the commands on the current line are executed.
1114.Pp
1115.It Ic default :
1116Labels the default case in a
1117.Ic switch
1118statement.
1119The default should come after all
1120.Ic case
1121labels.
1122.Pp
1123.It Ic dirs
1124Prints the directory stack; the top of the stack is at the left,
1125the first directory in the stack being the current directory.
1126.Pp
1127.It Ic echo Ar wordlist
1128.It Ic echo Fl n Ar wordlist
1129The specified words are written to the shells standard output, separated
1130by spaces, and terminated with a newline unless the
1131.Fl n
1132option is specified.
1133.Pp
1134.It Ic else
1135.It Ic end
1136.It Ic endif
1137.It Ic endsw
1138See the description of the
1139.Ic foreach ,
1140.Ic if ,
1141.Ic switch ,
1142and
1143.Ic while
1144statements below.
1145.Pp
1146.It Ic eval Ar arg ...
1147(As in
1148.Xr sh 1 . )
1149The arguments are read as input to the shell and the resulting
1150command(s) executed in the context of the current shell.
1151This is usually used to execute commands
1152generated as the result of command or variable substitution, since
1153parsing occurs before these substitutions. See
1154.Xr tset 1
1155for an example of using
1156.Ic eval .
1157.Pp
1158.It Ic exec Ar command
1159The specified command is executed in place of the current shell.
1160.Pp
1161.It Ic exit
1162.It Ic exit Ar (expr )
1163The shell exits either with the value of the
1164.Ic status
1165variable (first form) or with the value of the specified
1166.Ic expr
1167(second form).
1168.Pp
1169.It Ic fg
1170.It Ic fg \&% Ar job ...
1171Brings the current or specified jobs into the foreground, continuing them if
1172they were stopped.
1173.Pp
1174.It Ic foreach Ar name (wordlist)
1175.It ...
1176.It Ic end
1177The variable
1178.Ic name
1179is successively set to each member of
1180.Ic wordlist
1181and the sequence of commands between this command and the matching
1182.Ic end
1183are executed.
1184(Both
1185.Ic foreach
1186and
1187.Ic end
1188must appear alone on separate lines.)
1189The builtin command
1190.Ic continue
1191may be used to continue the loop prematurely and the builtin
1192command
1193.Ic break
1194to terminate it prematurely.
1195When this command is read from the terminal, the loop is read up once
1196prompting with `?' before any statements in the loop are executed.
1197If you make a mistake typing in a loop at the terminal you can rub it out.
1198.Pp
1199.It Ic glob Ar wordlist
1200Like
1201.Ic echo
1202but no `\e' escapes are recognized and words are delimited
1203by null characters in the output.
1204Useful for programs which wish to use the shell to filename expand a list
1205of words.
1206.Pp
1207.It Ic goto Ar word
1208The specified
1209.Ic word
1210is filename and command expanded to yield a string of the form `label'.
1211The shell rewinds its input as much as possible
1212and searches for a line of the form `label:'
1213possibly preceded by blanks or tabs.
1214Execution continues after the specified line.
1215.Pp
1216.It Ic hashstat
1217Print a statistics line indicating how effective the internal hash
1218table has been at locating commands (and avoiding
1219.Ic exec Ns \'s ) .
1220An
1221.Ic exec
1222is attempted for each component of the
1223.Em path
1224where the hash function indicates a possible hit, and in each component
1225which does not begin with a `/'.
1226.Pp
1227.It Ic history
1228.It Ic history Ar n
1229.It Ic history Fl r Ar n
1230.It Ic history Fl h Ar n
1231Displays the history event list; if
1232.Ar n
1233is given only the
1234.Ar n
1235most recent events are printed.
1236The
1237.Fl r
1238option reverses the order of printout to be most recent first
1239rather than oldest first.
1240The
1241.Fl h
1242option causes the history list to be printed without leading numbers.
1243This is used to produce files suitable for sourceing using the \-h
1244option to
1245.Ic source .
1246.Pp
1247.It Ic if Pq Ar expr No command
1248If the specified expression evaluates true, then the single
1249.Ar command
1250with arguments is executed.
1251Variable substitution on
1252.Ar command
1253happens early, at the same
1254time it does for the rest of the
1255.Ic if
1256command.
1257.Ar Command
1258must be a simple command, not
1259a pipeline, a command list, or a parenthesized command list.
1260Input/output redirection occurs even if
1261.Ar expr
1262is false, when command is
1263.Sy not
1264executed (this is a bug).
1265.Pp
1266.It Ic if ( Ar expr ) Ic then
1267.It ...
1268.It Ic else if ( Ar expr2 ) Ic then
1269.It ...
1270.It Ic else
1271.It ...
1272.It Ic endif
1273If the specified
1274.Ar expr
1275is true then the commands to the first
1276.Ic else
1277are executed; otherwise if
1278.Ar expr2
1279is true then the commands to the
1280second
1281.Ic else
1282are executed, etc.
1283Any number of
1284.Ic else-if
1285pairs are possible; only one
1286.Ic endif
1287is needed.
1288The
1289.Ic else
1290part is likewise optional.
1291(The words
1292.Ic else
1293and
1294.Ic endif
1295must appear at the beginning of input lines;
1296the
1297.Ic if
1298must appear alone on its input line or after an
1299.Ic else . )
1300.Pp
1301.It Ic jobs
1302.It Ic jobs Fl l
1303Lists the active jobs; given the
1304.Fl l
1305options lists process id's in addition to the normal information.
1306.Pp
1307.It Ic kill % Ar job
1308.It Ic kill Ar pid
1309.It Ic kill Fl sig Ar pid ...
1310.It Ic kill Fl l
1311Sends either the TERM (terminate) signal or the
1312specified signal to the specified jobs or processes.
1313Signals are either given by number or by names (as given in
1314.Pa /usr/include/signal.h,
1315stripped of the prefix ``SIG'').
1316The signal names are listed by ``kill \-l''.
1317There is no default, saying just `kill' does not
1318send a signal to the current job.
1319If the signal being sent is TERM (terminate) or HUP (hangup),
1320then the job or process will be sent a CONT (continue) signal as well.
1321.Pp
1322.It Ic limit
1323.It Ic limit Ar resource
1324.It Ic limit Ar resource maximum-use
1325.It Ic limit Fl h
1326.It Ic limit Fl h Ar resource
1327.It Ic limit Fl h Ar resource maximum-use
1328Limits the consumption by the current process and each process
1329it creates to not individually exceed
1330.Ar maximum-use
1331on the
1332specified
1333.Ar resource .
1334If no
1335.Ar maximum-use
1336is given, then
1337the current limit is printed; if no
1338.Ar resource
1339is given, then
1340all limitations are given. If the
1341.Fl h
1342flag is given, the hard limits are used instead of the current
1343limits. The hard limits impose a ceiling on the values of
1344the current limits. Only the super-user may raise the hard limits,
1345but a user may lower or raise the current limits within the legal range.
1346.Pp
1347Resources controllable currently include
1348.Ar cputime
1349(the maximum
1350number of cpu-seconds to be used by each process),
1351.Ar filesize
1352(the largest single file which can be created),
1353.Ar datasize
1354(the maximum growth of the data+stack region via
1355.Xr sbrk 2
1356beyond the end of the program text),
1357.Ar stacksize
1358(the maximum
1359size of the automatically-extended stack region), and
1360.Ar coredumpsize
1361(the size of the largest core dump that will be created).
1362.Pp
1363The
1364.Ar maximum-use
1365may be given as a (floating point or integer)
1366number followed by a scale factor. For all limits other than
1367.Ar cputime
1368the default scale is `k' or `kilobytes' (1024 bytes);
1369a scale factor of `m' or `megabytes' may also be used.
1370For
1371.Ar cputime
1372the default scaling is `seconds', while `m' for minutes
1373or `h' for hours, or a time of the form `mm:ss' giving minutes
1374and seconds may be used.
1375.Pp
1376For both
1377.Ar resource
1378names and scale factors, unambiguous prefixes
1379of the names suffice.
1380.Pp
1381.It Ic login
1382Terminate a login shell, replacing it with an instance of
1383.Pa /bin/login.
1384This is one way to log off, included for compatibility with
1385.Xr sh 1 .
1386.Pp
1387.It Ic logout
1388Terminate a login shell.
1389Especially useful if
1390.Ic ignoreeof
1391is set.
1392.Pp
1393.It Ic nice
1394.It Ic nice Ar +number
1395.It Ic nice Ar command
1396.It Ic nice Ar +number command
1397The first form sets the
1398scheduling priority
1399for this shell to 4.
1400The second form sets the
1401priority
1402to the given
1403.Ar number .
1404The final two forms run command at priority 4 and
1405.Ar number
1406respectively.
1407The greater the number, the less cpu the process will get.
1408The super-user may specify negative priority by using `nice \-number ...'.
1409Command is always executed in a sub-shell, and the restrictions
1410placed on commands in simple
1411.Ic if
1412statements apply.
1413.Pp
1414.It Ic nohup
1415.It Ic nohup Ar command
1416The first form can be used in shell scripts to cause hangups to be
1417ignored for the remainder of the script.
1418The second form causes the specified command to be run with hangups
1419ignored.
1420All processes detached with `&' are effectively
1421.Ic nohup Ns \'ed .
1422.Pp
1423.It Ic notify
1424.It Ic notify % Ar job ...
1425Causes the shell to notify the user asynchronously when the status of the
1426current or specified jobs changes; normally notification is presented
1427before a prompt. This is automatic if the shell variable
1428.Ic notify
1429is set.
1430.Pp
1431.It Ic onintr
1432.It Ic onintr Fl
1433.It Ic onintr Ar label
1434Control the action of the shell on interrupts.
1435The first form restores the default action of the shell on interrupts
1436which is to terminate shell scripts or to return to the terminal command
1437input level.
1438The second form `onintr \-' causes all interrupts to be ignored.
1439The final form causes the shell to execute a `goto label' when
1440an interrupt is received or a child process terminates because
1441it was interrupted.
1442.Pp
1443In any case, if the shell is running detached and interrupts are
1444being ignored, all forms of
1445.Ic onintr
1446have no meaning and interrupts
1447continue to be ignored by the shell and all invoked commands.
1448.Pp
1449.It Ic popd
1450.It Ic popd Ar +n
1451Pops the directory stack, returning to the new top directory.
1452With an argument
1453.Ns \`+ Ar n Ns \'
1454discards the
1455.Ar n Ns \'th
1456entry in the stack.
1457The elements of the directory stack are numbered from 0 starting at the top.
1458.Pp
1459.It Ic pushd
1460.It Ic pushd Ar name
1461.It Ic pushd Ar n
1462With no arguments,
1463.Ic pushd
1464exchanges the top two elements of the directory stack.
1465Given a
1466.Ar name
1467argument,
1468.Ic pushd
1469changes to the new directory (ala
1470.Ic cd )
1471and pushes the old current working directory
1472(as in
1473.Ic csw )
1474onto the directory stack.
1475With a numeric argument, rotates the
1476.Ar n Ns \'th
1477argument of the directory
1478stack around to be the top element and changes to it. The members
1479of the directory stack are numbered from the top starting at 0.
1480.Pp
1481.It Ic rehash
1482Causes the internal hash table of the contents of the directories in
1483the
1484.Ic path
1485variable to be recomputed. This is needed if new commands are added
1486to directories in the
1487.Ic path
1488while you are logged in. This should only be necessary if you add
1489commands to one of your own directories, or if a systems programmer
1490changes the contents of one of the system directories.
1491.Pp
1492.It Ic repeat Ar count command
1493The specified
1494.Ar command
1495which is subject to the same restrictions
1496as the
1497.Ar command
1498in the one line
1499.Ic if
1500statement above,
1501is executed
1502.Ar count
1503times.
1504I/O redirections occur exactly once, even if
1505.Ar count
1506is 0.
1507.Pp
1508.It Ic set
1509.It Ic set Ar name
1510.It Ic set Ar name Ns =word
1511.It Ic set Ar name[index] Ns =word
1512.It Ic set Ar name Ns =(wordlist)
1513The first form of the command shows the value of all shell variables.
1514Variables which have other than a single word as value print as a parenthesized
1515word list.
1516The second form sets
1517.Ic name
1518to the null string.
1519The third form sets
1520.Ic name
1521to the single
1522.Ic word .
1523The fourth form sets
1524the
1525.Ar index Ns 'th
1526component of name to word;
1527this component must already exist.
1528The final form sets
1529.Ar name
1530to the list of words in
1531.Ar wordlist .
1532In all cases the value is command and filename expanded.
1533.Pp
1534These arguments may be repeated to set multiple values in a single set command.
1535Note however, that variable expansion happens for all arguments before any
1536setting occurs.
1537.Pp
1538.It Ic setenv
1539.It Ic setenv Ar name value
1540.It Ic setenv Ar name
1541The first form lists all current environment variables.
1542The last form sets the value of environment variable
1543.Ar name
1544to be
1545.Ar value ,
1546a single string. The second form sets
1547.Ar name
1548to an empty string.
1549The most commonly used environment variable
1550.Ev USER ,
1551.Ev TERM ,
1552and
1553.Ev PATH
1554are automatically imported to and exported from the
1555.Nm csh
1556variables
1557.Ar user ,
1558.Op Ar term ,
1559and
1560.Ar path ;
1561there is no need to use
1562.Ic setenv
1563for these.
1564.Pp
1565.It Ic shift
1566.It Ic shift Ar variable
1567The members of
1568.Ic argv
1569are shifted to the left, discarding
1570.Ic argv Bq 1 .
1571It is an error for
1572.Ic argv
1573not to be set or to have less than one word as value.
1574The second form performs the same function on the specified variable.
1575.Pp
1576.It Ic source Ar name
1577.It Ic source Fl h Ar name
1578The shell reads commands from
1579.Ic name .
1580.Ic Source
1581commands may be nested; if they are nested too deeply the shell may
1582run out of file descriptors.
1583An error in a
1584.Ic source
1585at any level terminates all nested
1586.Ic source
1587commands.
1588Normally input during
1589.Ic source
1590commands is not placed on the history list;
1591the \-h option causes the commands to be placed in the
1592history list without being executed.
1593.Pp
1594.It Ic stop
1595.It Ic stop % Ns Ar job ...
1596Stops the current or specified job which is executing in the background.
1597.Pp
1598.It Ic suspend
1599Causes the shell to stop in its tracks, much as if it had been sent a stop
1600signal with
1601.Ic ^Z .
1602This is most often used to stop shells started by
1603.Xr su 1 .
1604.Pp
1605.It Ic switch Ar (string)
1606.It Ic case Ar str1 :
1607.It \ \ \ \ \&...
1608.It Ic \ \ \ \ breaksw
1609.It \ \ \ \ \&...
1610.It Ic default :
1611.It \ \ \ \ \&...
1612.It Ic \ \ \ \ breaksw
1613.It Ic endsw
1614Each case label is successively matched, against the specified
1615.Ar string
1616which is first command and filename expanded.
1617The file metacharacters `*', `?' and `[...]'
1618may be used in the case labels,
1619which are variable expanded.
1620If none of the labels match before a `default' label is found, then
1621the execution begins after the default label.
1622Each case label and the default label must appear at the beginning of a line.
1623The command
1624.Ic breaksw
1625causes execution to continue after the
1626.Ic endsw .
1627Otherwise control may fall through case labels and default labels as in C.
1628If no label matches and there is no default, execution continues after
1629the
1630.Ic endsw .
1631.Pp
1632.It Ic time
1633.It Ic time Ar command
1634With no argument, a summary of time used by this shell and its children
1635is printed.
1636If arguments are given
1637the specified simple command is timed and a time summary
1638as described under the
1639.Ic time
1640variable is printed. If necessary, an extra shell is created to print the time
1641statistic when the command completes.
1642.Pp
1643.It Ic umask
1644.It Ic umask Ar value
1645The file creation mask is displayed (first form) or set to the specified
1646value (second form). The mask is given in octal. Common values for
1647the mask are 002 giving all access to the group and read and execute
1648access to others or 022 giving all access except no write access for
1649users in the group or others.
1650.Pp
1651.It Ic unalias Ar pattern
1652All aliases whose names match the specified pattern are discarded.
1653Thus all aliases are removed by `unalias *'.
1654It is not an error for nothing to be
1655.Ic unaliased .
1656.Pp
1657.It Ic unhash
1658Use of the internal hash table to speed location of executed programs
1659is disabled.
1660.Pp
1661.It Ic unlimit
1662.It Ic unlimit Ar resource
1663.It Ic unlimit Fl h
1664.It Ic unlimit Fl h Ar resource
1665Removes the limitation on
1666.Ar resource .
1667If no
1668.Ar resource
1669is specified, then all
1670.Ar resource
1671limitations are removed. If
1672.Fl h
1673is given, the corresponding hard limits are removed. Only the
1674super-user may do this.
1675.Pp
1676.It Ic unset Ar pattern
1677All variables whose names match the specified pattern are removed.
1678Thus all variables are removed by `unset *'; this has noticeably
1679distasteful side-effects.
1680It is not an error for nothing to be
1681.Ic unset .
1682.Pp
1683.It Ic unsetenv Ar pattern
1684Removes all variables whose name match the specified pattern from the
1685environment. See also the
1686.Ic setenv
1687command above and
1688.Xr printenv 1 .
1689.Pp
1690.It Ic wait
1691All background jobs are waited for.
1692It the shell is interactive, then an interrupt can disrupt the wait,
1693at which time the shell prints names and job numbers of all jobs
1694known to be outstanding.
1695.Pp
1696.It Ic while Ar (expr)
1697.It \&...
1698.It Ic end
1699While the specified expression evaluates non-zero, the commands between
1700the
1701.Ic while
1702and the matching end are evaluated.
1703.Ic Break
1704and
1705.Ic continue
1706may be used to terminate or continue the loop prematurely.
1707(The
1708.Ic while
1709and
1710.Ic end
1711must appear alone on their input lines.)
1712Prompting occurs here the first time through the loop as for the
1713.Ic foreach
1714statement if the input is a terminal.
1715.Pp
1716.It Ic % Ar job
1717Brings the specified job into the foreground.
1718.Pp
1719.It Ic % Ar job Ic &
1720Continues the specified job in the background.
1721.Pp
1722.It Ic @
1723.It Ic @ Ns Ar name Ns = expr
1724.It Ic @ Ns Ar name[index] Ns = expr
1725The first form prints the values of all the shell variables.
1726The second form sets the specified
1727.Ar name
1728to the value of
1729.Ar expr .
1730If the expression contains `<', `>', `&' or `' then at least
1731this part of the expression must be placed within `(' `)'.
1732The third form assigns the value of
1733.Ar expr
1734to the
1735.Ar index Ns 'th
1736argument of
1737.Ar name .
1738Both
1739.Ar name
1740and its
1741.Ar index Ns 'th
1742component must already exist.
1743.El
1744.Pp
1745The operators `*=', `+=', etc are available as in C.
1746The space separating the name from the assignment operator is optional.
1747Spaces are, however, mandatory in separating components of
1748.Ar expr
1749which would otherwise be single words.
1750.Pp
1751Special postfix `++' and `\-\-' operators increment and decrement
1752.Ar name
1753respectively, i.e. `@ i++'.
1754.Ss Pre-defined and environment variables
1755The following variables have special meaning to the shell.
1756Of these,
1757.Ar argv ,
1758.Ar cwd,
1759.Ar home ,
1760.Ar path,
1761.Ar prompt ,
1762.Ar shell
1763and
1764.Ar status
1765are always set by the shell.
1766Except for
1767.Ar cwd
1768and
1769.Ar status
1770this setting occurs only at initialization;
1771these variables will not then be modified unless this is done
1772explicitly by the user.
1773.Pp
1774This shell copies the environment variable
1775.Ev USER
1776into the variable
1777.Ar user ,
1778.Ev TERM
1779into
1780.Ar term ,
1781and
1782.Ev HOME
1783into
1784.Ar home ,
1785and copies these back into the environment whenever the normal
1786shell variables are reset.
1787The environment variable
1788.Ev PATH
1789is likewise handled; it is not
1790necessary to worry about its setting other than in the file
1791.Ar \&.cshrc
1792as inferior
1793.Nm csh
1794processes will import the definition of
1795.Ar path
1796from the environment, and re-export it if you then change it.
1797.Bl -tag -width histchars
1798.It Ic argv
1799Set to the arguments to the shell, it is from this variable that
1800positional parameters are substituted, i.e. `$1' is replaced by
1801`$argv[1]',
1802etc.
1803.It Ic cdpath
1804Gives a list of alternate directories searched to find subdirectories
1805in
1806.Ar chdir
1807commands.
1808.It Ic cwd
1809The full pathname of the current directory.
1810.It Ic echo
1811Set when the
1812.Fl x
1813command line option is given.
1814Causes each command and its arguments
1815to be echoed just before it is executed.
1816For non-builtin commands all expansions occur before echoing.
1817Builtin commands are echoed before command and filename substitution,
1818since these substitutions are then done selectively.
1819.It Ic filec
1820Enable file name completion.
1821.It Ic histchars
1822Can be given a string value to change the characters used in history
1823substitution. The first character of its value is used as the
1824history substitution character, replacing the default character `!'.
1825The second character of its value replaces the character `\(ua' in
1826quick substitutions.
1827.It Ic history
1828Can be given a numeric value to control the size of the history list.
1829Any command which has been referenced in this many events will not be
1830discarded.
1831Too large values of
1832.Ar history
1833may run the shell out of memory.
1834The last executed command is always saved on the history list.
1835.It Ic home
1836The home directory of the invoker, initialized from the environment.
1837The filename expansion of
1838.Sq Pa ~
1839refers to this variable.
1840.It Ic ignoreeof
1841If set the shell ignores
1842end-of-file from input devices which are terminals.
1843This prevents shells from accidentally being killed by control-D's.
1844.It Ic mail
1845The files where the shell checks for mail.
1846This is done after each command completion which will result in a prompt,
1847if a specified interval has elapsed.
1848The shell says `You have new mail.'
1849if the file exists with an access time not greater than its modify time.
1850.Pp
1851If the first word of the value of
1852.Ar mail
1853is numeric it specifies a different mail checking interval, in seconds,
1854than the default, which is 10 minutes.
1855.Pp
1856If multiple mail files are specified, then the shell says
1857`New mail in
1858.Ar name Ns '
1859when there is mail in the file
1860.Ar name .
1861.It Ic noclobber
1862As described in the section on
1863.Sx Input/output ,
1864restrictions are placed on output redirection to insure that
1865files are not accidentally destroyed, and that `>>' redirections
1866refer to existing files.
1867.It Ic noglob
1868If set, filename expansion is inhibited.
1869This is most useful in shell scripts which are not dealing with filenames,
1870or after a list of filenames has been obtained and further expansions
1871are not desirable.
1872.It Ic nonomatch
1873If set, it is not an error for a filename expansion to not match any
1874existing files; rather the primitive pattern is returned.
1875It is still an error for the primitive pattern to be malformed, i.e.
1876`echo ['
1877still gives an error.
1878.It Ic notify
1879If set, the shell notifies asynchronously of job completions. The
1880default is to rather present job completions just before printing
1881a prompt.
1882.It Ic path
1883Each word of the path variable specifies a directory in which
1884commands are to be sought for execution.
1885A null word specifies the current directory.
1886If there is no
1887.Ar path
1888variable then only full path names will execute.
1889The usual search path is `.', `/bin' and `/usr/bin', but this
1890may vary from system to system.
1891For the super-user the default search path is `/etc', `/bin' and `/usr/bin'.
1892A shell which is given neither the
1893.Fl c
1894nor the
1895.Fl t
1896option will normally hash the contents of the directories in the
1897.Ar path
1898variable after reading
1899.Ar \&.cshrc ,
1900and each time the
1901.Ar path
1902variable is reset. If new commands are added to these directories
1903while the shell is active, it may be necessary to do a
1904.Ic rehash
1905or the commands may not be found.
1906.It Ic prompt
1907The string which is printed before each command is read from
1908an interactive terminal input.
1909If a `!' appears in the string it will be replaced by the current event number
1910unless a preceding `\e' is given.
1911Default is `% ', or `# ' for the super-user.
1912.It Ic savehist
1913Is given a numeric value to control the number of entries of the
1914history list that are saved in ~/.history when the user logs out.
1915Any command which has been referenced in this many events will be saved.
1916During start up the shell sources ~/.history into the history list
1917enabling history to be saved across logins.
1918Too large values of
1919.Ar savehist
1920will slow down the shell during start up.
1921.It Ic shell
1922The file in which the shell resides.
1923This is used in forking shells to interpret files which have execute
1924bits set, but which are not executable by the system.
1925(See the description of
1926.Sx Non-builtin Command Execution
1927below.)
1928Initialized to the (system-dependent) home of the shell.
1929.It Ic status
1930The status returned by the last command.
1931If it terminated abnormally, then 0200 is added to the status.
1932Builtin commands which fail return exit status `1',
1933all other builtin commands set status `0'.
1934.It Ic time
1935Controls automatic timing of commands.
1936If set, then any command which takes more than this many cpu seconds
1937will cause a line giving user, system, and real times and a utilization
1938percentage which is the ratio of user plus system times to real time
1939to be printed when it terminates.
1940.It Ic verbose
1941Set by the
1942.Fl v
1943command line option, causes the words of each command to be printed
1944after history substitution.
1945.El
1946.Ss Non-builtin command execution
1947When a command to be executed is found to not be a builtin command
1948the shell attempts to execute the command via
1949.Xr execve 2 .
1950Each word in the variable
1951.Ar path
1952names a directory from which the shell will attempt to execute the command.
1953If it is given neither a
1954.Fl c
1955nor a
1956.Fl t
1957option, the shell will hash the names in these directories into an internal
1958table so that it will only try an
1959.Ic exec
1960in a directory if there is a possibility that the command resides there.
1961This greatly speeds command location when a large number of directories
1962are present in the search path.
1963If this mechanism has been turned off (via
1964.Ic unhash ) ,
1965or if the shell was given a
1966.Fl c
1967or
1968.Fl t
1969argument, and in any case for each directory component of
1970.Ar path
1971which does not begin with a `/',
1972the shell concatenates with the given command name to form a path name
1973of a file which it then attempts to execute.
1974.Pp
1975Parenthesized commands are always executed in a subshell.
1976Thus
1977.Pp
1978.Dl (cd ; pwd) ; pwd
1979.Pp
1980prints the
1981.Ar home
1982directory; leaving you where you were (printing this after the home directory),
1983while
1984.Pp
1985.Dl cd ; pwd
1986.Pp
1987leaves you in the
1988.Ar home
1989directory.
1990Parenthesized commands are most often used to prevent
1991.Ic chdir
1992from affecting the current shell.
1993.Pp
1994If the file has execute permissions but is not an
1995executable binary to the system, then it is assumed to be a
1996file containing shell commands and a new shell is spawned to read it.
1997.Pp
1998If there is an
1999.Ic alias
2000for
2001.Ic shell
2002then the words of the alias will be prepended to the argument list to form
2003the shell command.
2004The first word of the
2005.Ic alias
2006should be the full path name of the shell
2007(e.g. `$shell').
2008Note that this is a special, late occurring, case of
2009.Ic alias
2010substitution,
2011and only allows words to be prepended to the argument list without modification.
2012.Ss Signal handling
2013The shell normally ignores
2014.Ar quit
2015signals.
2016Jobs running detached (either by
2017.Ic \&&
2018or the
2019.Ic bg
2020or
2021.Ic %... &
2022commands) are immune to signals generated from the keyboard, including
2023hangups.
2024Other signals have the values which the shell inherited from its parent.
2025The shell's handling of interrupts and terminate signals
2026in shell scripts can be controlled by
2027.Ic onintr .
2028Login shells catch the
2029.Ar terminate
2030signal; otherwise this signal is passed on to children from the state in the
2031shell's parent.
2032In no case are interrupts allowed when a login shell is reading the file
2033.Pa \&.logout .
2034.Sh AUTHOR
2035William Joy.
2036Job control and directory stack features first implemented by J.E. Kulp of
2037IIASA, Laxenburg, Austria,
2038with different syntax than that used now.
2039File name completion code written by Ken Greer, HP Labs.
2040Eight-bit implementation Christos S. Zoulas, Cornell University.
2041.Sh FILES
2042.Bl -tag -width /etc/passwd -compact
2043.It Pa ~/.cshrc
2044Read at beginning of execution by each shell.
2045.It Pa ~/.login
2046Read by login shell, after `.cshrc' at login.
2047.It Pa ~/.logout
2048Read by login shell, at logout.
2049.It Pa /bin/sh
2050Standard shell, for shell scripts not starting with a `#'.
2051.It Pa /tmp/sh*
2052Temporary file for `<<'.
2053.It Pa /etc/passwd
2054Source of home directories for `~name'.
2055.El
2056.Sh LIMITATIONS
2057Word lengths \-
2058Words can be no longer than 1024 characters.
2059The system limits argument lists to 10240 characters.
2060The number of arguments to a command which involves filename expansion
2061is limited to 1/6'th the number of characters allowed in an argument list.
2062Command substitutions may substitute no more characters than are
2063allowed in an argument list.
2064To detect looping, the shell restricts the number of
2065.Ic alias
2066substitutions on a single line to 20.
2067.Sh SEE ALSO
2068.Xr sh 1 ,
2069.Xr access 2 ,
2070.Xr execve 2 ,
2071.Xr fork 2 ,
2072.Xr killpg 2 ,
2073.Xr pipe 2 ,
2074.Xr sigvec 2 ,
2075.Xr umask 2 ,
2076.Xr setrlimit 2 ,
2077.Xr wait 2 ,
2078.Xr tty 4 ,
2079.Xr a.out 5 ,
2080.Xr environ 7 ,
2081.br
2082.Em An introduction to the C shell
2083.Sh HISTORY
2084.Nm Csh
2085appeared in
2086.Bx 3 .
2087It
2088was a first implementation of a command language interpreter
2089incorporating a history mechanism (see
2090.Sx History Substitutions ) ,
2091job control facilities (see
2092.Sx Jobs ) ,
2093interactive file name
2094and user name completion (see
2095.Sx File Name Completion ) ,
2096and a C-like syntax.
2097There are now many shells which also have these mechanisms, plus
2098a few more (and maybe some bugs too), which are available thru the
2099usenet, or with
2100.Bx
2101as contributed software like the
2102.Xr ksh korn\ shell .
2103.Sh BUGS
2104When a command is restarted from a stop,
2105the shell prints the directory it started in if this is different
2106from the current directory; this can be misleading (i.e. wrong)
2107as the job may have changed directories internally.
2108.Pp
2109Shell builtin functions are not stoppable/restartable.
2110Command sequences of the form `a ; b ; c' are also not handled gracefully
2111when stopping is attempted. If you suspend `b', the shell will then
2112immediately execute `c'. This is especially noticeable if this
2113expansion results from an
2114.Ar alias .
2115It suffices to place the sequence of commands in ()'s to force it to
2116a subshell, i.e. `( a ; b ; c )'.
2117.Pp
2118Control over tty output after processes are started is primitive;
2119perhaps this will inspire someone to work on a good virtual
2120terminal interface. In a virtual terminal interface much more
2121interesting things could be done with output control.
2122.Pp
2123Alias substitution is most often used to clumsily simulate shell procedures;
2124shell procedures should be provided rather than aliases.
2125.Pp
2126Commands within loops, prompted for by `?', are not placed in the
2127.Ic history
2128list.
2129Control structure should be parsed rather than being recognized as built-in
2130commands. This would allow control commands to be placed anywhere,
2131to be combined with `\&|', and to be used with `&' and `;' metasyntax.
2132.Pp
2133It should be possible to use the `:' modifiers on the output of command
2134substitutions.
2135All and more than one `:' modifier should be allowed on `$' substitutions.
2136.Pp
2137The way the
2138.Ic filec
2139facility is implemented is ugly and expensive.