BSD 3 development
[unix-history] / usr / doc / csh / csh.4
CommitLineData
b378a021
BJ
1.nr H1 3
2.NH
3Other, less commonly used, shell features
4.NH 2
5Loops at the terminal; variables as vectors
6.PP
7It is occasionally useful to use the
8.I foreach
9control structure at the terminal to aid in performing a number
10of similar commands.
11For instance, there were at one point three shells in use on the Cory \s-2UNIX\s0
12system at Cory Hall,
13`/bin/sh',
14`/bin/nsh',
15and
16`/bin/csh'.
17To count the number of persons using each shell one could have issued
18the commands
19.DS
20% grep \-c csh$ /etc/passwd
2127
22% grep \-c nsh$ /etc/passwd
23128
24% grep \-c \-v sh$ /etc/passwd
25430
26%
27.DE
28Since these commands are very similar we can use
29.I foreach
30to do this more easily.
31.DS
32% foreach i (\'sh$\' \'csh$\' \'\-v sh$\')
33? grep \-c $i /etc/passwd
34? end
3527
36128
37430
38%
39.DE
40Note here that the shell prompts for
41input with `? ' when reading the body of the loop.
42.PP
43Very useful with loops are variables which contain lists of filenames
44or other words.
45You can, for example, do
46.DS
47% set a=(\`ls\`)
48% echo $a
49csh.n csh.rm
50% ls
51csh.n
52csh.rm
53% echo $#a
542
55%
56.DE
57The
58.I set
59command here gave the variable
60.I a
61a list of all the filenames in the current directory as value.
62We can then iterate over these names to perform any chosen function.
63.PP
64The output of a command within `\`' characters is converted by
65the shell to a list of words.
66You can also place the `\`' quoted string within `"' characters
67to take each (non-empty) line as a component of the variable;
68preventing the lines from being split into words at blanks and tabs.
69A modifier `:x' exists which can be used later to expand each component
70of the variable into another variable splitting it into separate words
71at embedded blanks and tabs.
72.NH 2
73Braces { ... } in argument expansion
74.PP
75Another form of filename expansion, alluded
76to before involves the characters `{' and `}'.
77These characters specify that the contained strings, separated by `,'
78are to be consecutively substituted into the containing characters
79and the results expanded left to right.
80Thus
81.DS
82A{str1,str2,...strn}B
83.DE
84expands to
85.DS
86Astr1B Astr2B ... AstrnB
87.DE
88This expansion occurs before the other filename expansions, and may
89be applied recursively (i.e. nested).
90The results of each expanded string are sorted separately, left
91to right order being preserved.
92The resulting filenames are not required to exist if no other expansion
93mechanisms are used.
94This means that this mechanism can be used to generate arguments which are
95not filenames, but which have common parts.
96.PP
97A typical use of this would be
98.DS
99mkdir ~/{hdrs,retrofit,csh}
100.DE
101to make subdirectories `hdrs', `retrofit' and `csh'
102in your home directory.
103This mechanism is most useful when the common prefix is longer
104than in this example, i.e.
105.DS
106chown bin /usr/{bin/{ex,edit},lib/{ex1.1strings,how_ex}}
107.DE
108.NH 2
109Command substitution
110.PP
111A command enclosed in `\`' characters is replaced, just before
112filenames are expanded, by the output from that command.
113Thus it is possible to do
114.DS
115set pwd=\`pwd\`
116.DE
117to save the current directory in the variable
118.I pwd
119or to do
120.DS
121ex \`grep \-l TRACE *.c\`
122.DE
123to run the editor
124.I ex
125suppling as arguments those files whose names end in `.c'
126which have the string `TRACE' in them.*
127.FS
128*Command expansion also occurs in input redirected with `<<'
129and within `"' quotations.
130Refer to the shell manual section for full details.
131.FE
132.NH 2
133Other details not covered here
134.PP
135In particular circumstances it may be necessary to know the exact
136nature and order of different substitutions performed by the shell.
137The exact meaning of certain combinations of quotations is also
138occasionally important.
139These are detailed fully in its manual section.
140.PP
141The shell has a number of command line option flags mostly of use
142in writing \s-2UNIX\s0 programs,
143and debugging shell scripts.
144See the shells manual section for a list of these options.
145.bp