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