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