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