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