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