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