Merge branch 'vendor/LIBPCAP' (early part)
[dragonfly.git] / bin / csh / USD.doc / csh.4
1 .\" Copyright (c) 1980, 1993
2 .\"     The Regents of the University of California.  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.4       8.1 (Berkeley) 6/8/93
33 .\" $FreeBSD: src/bin/csh/USD.doc/csh.4,v 1.5 1999/08/27 23:13:55 peter Exp $
34 .\" $DragonFly: src/bin/csh/USD.doc/csh.4,v 1.2 2003/06/17 04:22:49 dillon Exp $
35 .\"
36 .nr H1 3
37 .NH
38 Other, less commonly used, shell features
39 .NH 2
40 Loops at the terminal; variables as vectors
41 .PP
42 It is occasionally useful to use the
43 .I foreach
44 control structure at the terminal to aid in performing a number
45 of similar commands.
46 For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0
47 system at Cory Hall,
48 `/bin/sh',
49 `/bin/nsh',
50 and
51 `/bin/csh'.
52 To count the number of persons using each shell one could have issued
53 the commands
54 .DS
55 % grep \-c csh$ /etc/passwd
56 27
57 % grep \-c nsh$ /etc/passwd
58 128
59 % grep \-c \-v sh$ /etc/passwd
60 430
61 %
62 .DE
63 Since these commands are very similar we can use
64 .I foreach
65 to do this more easily.
66 .DS
67 % foreach i (\'sh$\' \'csh$\' \'\-v sh$\')
68 ? grep \-c $i /etc/passwd
69 ? end
70 27
71 128
72 430
73 %
74 .DE
75 Note here that the shell prompts for
76 input with `? ' when reading the body of the loop.
77 .PP
78 Very useful with loops are variables which contain lists of filenames
79 or other words.
80 You can, for example, do
81 .DS
82 % set a=(\`ls\`)
83 % echo $a
84 csh.n csh.rm
85 % ls
86 csh.n
87 csh.rm
88 % echo $#a
89 2
90 %
91 .DE
92 The
93 .I set
94 command here gave the variable
95 .I a
96 a list of all the filenames in the current directory as value.
97 We can then iterate over these names to perform any chosen function.
98 .PP
99 The output of a command within `\`' characters is converted by
100 the shell to a list of words.
101 You can also place the `\`' quoted string within `"' characters
102 to take each (non-empty) line as a component of the variable;
103 preventing the lines from being split into words at blanks and tabs.
104 A modifier `:x' exists which can be used later to expand each component
105 of the variable into another variable splitting it into separate words
106 at embedded blanks and tabs.
107 .NH 2
108 Braces { ... } in argument expansion
109 .PP
110 Another form of filename expansion, alluded
111 to before involves the characters `{' and `}'.
112 These characters specify that the contained strings, separated by `,'
113 are to be consecutively substituted into the containing characters
114 and the results expanded left to right.
115 Thus
116 .DS
117 A{str1,str2,...strn}B
118 .DE
119 expands to
120 .DS
121 Astr1B Astr2B ... AstrnB
122 .DE
123 This expansion occurs before the other filename expansions, and may
124 be applied recursively (i.e. nested).
125 The results of each expanded string are sorted separately, left
126 to right order being preserved.
127 The resulting filenames are not required to exist if no other expansion
128 mechanisms are used.
129 This means that this mechanism can be used to generate arguments which are
130 not filenames, but which have common parts.
131 .PP
132 A typical use of this would be
133 .DS
134 mkdir ~/{hdrs,retrofit,csh}
135 .DE
136 to make subdirectories `hdrs', `retrofit' and `csh'
137 in your home directory.
138 This mechanism is most useful when the common prefix is longer
139 than in this example, i.e.
140 .DS
141 chown root /usr/{ucb/{ex,edit},lib/{ex?.?*,how_ex}}
142 .DE
143 .NH 2
144 Command substitution
145 .PP
146 A command enclosed in `\`' characters is replaced, just before
147 filenames are expanded, by the output from that command.
148 Thus it is possible to do
149 .DS
150 set pwd=\`pwd\`
151 .DE
152 to save the current directory in the variable
153 .I pwd
154 or to do
155 .DS
156 ex \`grep \-l TRACE *.c\`
157 .DE
158 to run the editor
159 .I ex
160 supplying as arguments those files whose names end in `.c'
161 which have the string `TRACE' in them.*
162 .FS
163 *Command expansion also occurs in input redirected with `<<'
164 and within `"' quotations.
165 Refer to the shell manual section for full details.
166 .FE
167 .NH 2
168 Other details not covered here
169 .PP
170 In particular circumstances it may be necessary to know the exact
171 nature and order of different substitutions performed by the shell.
172 The exact meaning of certain combinations of quotations is also
173 occasionally important.
174 These are detailed fully in its manual section.
175 .PP
176 The shell has a number of command line option flags mostly of use
177 in writing \s-2UNIX\s0 programs,
178 and debugging shell scripts.
179 See the csh(1) manual section for a list of these options.
180 .bp