Commit | Line | Data |
---|---|---|
fde98a2d BJ |
1 | .th SH I 10/28/76 |
2 | .sh NAME | |
3 | sh \*- shell (command interpreter) | |
4 | .sh SYNOPSIS | |
5 | .bd sh | |
6 | [ | |
7 | .bd \*-tciv | |
8 | ] | |
9 | [ name [ arg1 ... [ arg9 ... ] ] ] | |
10 | .sh DESCRIPTION | |
11 | .it Sh | |
12 | is the standard command interpreter. | |
13 | It is the program which reads and arranges the execution of | |
14 | the command lines typed by most users. | |
15 | It may itself be called as a command to interpret | |
16 | files of commands. | |
17 | Before discussing the arguments to the Shell | |
18 | used as a command, the structure of command | |
19 | lines themselves will be given. | |
20 | .s3 | |
21 | .bd "Commands." | |
22 | Each command is a sequence of non-blank command arguments | |
23 | separated by blanks. | |
24 | The | |
25 | first argument specifies the name of a command to be | |
26 | executed. | |
27 | Except for certain types of special | |
28 | arguments discussed below, the arguments | |
29 | other than the command name are passed | |
30 | without interpretation to the invoked | |
31 | command. | |
32 | .s3 | |
33 | If the first argument is the name of an executable | |
34 | file, it is invoked; | |
35 | If the first argument is not the name of an executable file, | |
36 | and it does not contain the character `/', the shell will search | |
37 | other directories for the command. First it tries each user-specified library | |
38 | (see lib (I)). If it still has not found the command, | |
39 | tries the standard system library `/bin'. | |
40 | (In this way most of the standard UNIX commands, | |
41 | which reside in `/bin', are found.) | |
42 | If this fails, it finally tries | |
43 | the library `/usr/bin', | |
44 | where lesser-used UNIX commands live. | |
45 | .s3 | |
46 | If a non-directory file has executable mode, | |
47 | but not the form of an executable program | |
48 | (does not begin with the proper magic number) | |
49 | then it is examined to see if it is a Pascal object | |
50 | (first word 0404 - a magic number). If it is, then | |
51 | a new pascal interpreter is created to interpret it. | |
52 | Otherwise, it is assumed to be an ASCII file of commands | |
53 | and a new Shell is created to execute it. | |
54 | See ``Argument passing'' below. | |
55 | .s3 | |
56 | If the file cannot be found, | |
57 | a diagnostic is printed. | |
58 | .s3 | |
59 | .bd "Command lines." | |
60 | One or more commands separated by `|' or `^' constitute a | |
61 | chain of | |
62 | .it filters. | |
63 | The standard output of each command but the last | |
64 | is taken | |
65 | as the standard input of the next command. | |
66 | Each command is run as a separate process, connected | |
67 | by pipes (see pipe(II)) to its neighbors. | |
68 | A command line contained in | |
69 | parentheses `( )' may appear in place of a simple command | |
70 | as a filter. | |
71 | .s3 | |
72 | A | |
73 | .it "command line" | |
74 | consists of one or | |
75 | more pipelines separated, and perhaps terminated by `\fB;\fR' or `&'. | |
76 | The semicolon designates sequential execution. | |
77 | The `&' causes the preceding command line to be executed | |
78 | without waiting for it to finish. | |
79 | The process id of such a command line is reported, so that | |
80 | it may be used if necessary for a subsequent | |
81 | .it wait | |
82 | or | |
83 | .it kill. | |
84 | If, however, the `&' is followed by a `*', i.e. `&*', then | |
85 | the preceding set of `;' separated commands is | |
86 | put in the background, as a group. Thus `cc sh.c ; mv a.out sh &*' is equivalent | |
87 | to `(cc sh.c ; mv a.out sh)&'. | |
88 | .s3 | |
89 | .bd "Termination Reporting." | |
90 | If a command (not followed by `&') terminates abnormally, | |
91 | a message is printed. | |
92 | (All terminations other than exit and interrupt | |
93 | are considered abnormal.) | |
94 | Termination reports for commands followed by `&' | |
95 | are given upon receipt of the first | |
96 | command subsequent to the termination of | |
97 | the command, | |
98 | or when a | |
99 | .it wait | |
100 | is executed. | |
101 | The following is a list of the abnormal | |
102 | termination messages: | |
103 | .s3 | |
104 | .nf | |
105 | Bus error | |
106 | Trace/BPT trap | |
107 | Illegal instruction | |
108 | IOT trap | |
109 | EMT trap | |
110 | Bad system call | |
111 | Quit | |
112 | Floating exception | |
113 | Memory fault | |
114 | Killed | |
115 | .s3 | |
116 | .fi | |
117 | If a core image is produced, | |
118 | `\*- Core dumped' is appended to the appropriate message. | |
119 | .s3 | |
120 | .bd "Redirection of I/O." | |
121 | There are three character sequences that cause the immediately following string | |
122 | to be interpreted as a special argument to the Shell itself. | |
123 | Such an argument may | |
124 | appear anywhere among | |
125 | the arguments of a simple command, or before or after | |
126 | a parenthesized command list, and is associated with that | |
127 | command or command list. | |
128 | .s3 | |
129 | An argument of the form `<file' causes the file | |
130 | `file' | |
131 | to be used as the standard input (file descriptor 0) of the associated command. | |
132 | .s3 | |
133 | An argument of the form `>file' causes file `file' to be used | |
134 | as the standard output (file descriptor 1) for the associated command. | |
135 | `File' is created if it did not exist, and in any case is truncated | |
136 | at the outset. | |
137 | .s3 | |
138 | An argument of the form `>>file' causes file `file' to be used as the | |
139 | standard output for the associated command. | |
140 | If `file' | |
141 | did not exist, it is created; if it did exist, | |
142 | the command output is appended to the file. | |
143 | .s3 | |
144 | For example, either of the command lines | |
145 | .s3 | |
146 | ls >junk; cat tail >>junk | |
147 | .br | |
148 | ( ls; cat tail ) >junk | |
149 | .s3 | |
150 | creates, on file `junk', a listing of the working directory, followed immediately | |
151 | by the contents of file `tail'. | |
152 | .s3 | |
153 | Either of the constructs `>file' or `>>file' | |
154 | associated with any but the last command of a pipeline | |
155 | is ineffectual, as is `<file' in any but the first. | |
156 | .s3 | |
157 | In commands called by the Shell, | |
158 | file descriptor 2 refers to the standard output of the | |
159 | Shell before any redirection. | |
160 | Thus filters may write diagnostics | |
161 | to a location | |
162 | where they have a chance to be seen. | |
163 | If is not desired that they be seen, the forms `>* file', | |
164 | `>>* file', and `|* ...' cause both units 1 and 2 to refer | |
165 | to the specified file or put through the pipe. | |
166 | .s3 | |
167 | .bd "Generation of argument lists." | |
168 | If any argument contains any of the characters `?', | |
169 | `*' or `[', it is treated specially as follows. | |
170 | The current directory is searched for files which | |
171 | .it match | |
172 | the given argument. | |
173 | .s3 | |
174 | The character `*' in an argument matches any string of characters | |
175 | in a file name (including the null string). | |
176 | .s3 | |
177 | The character `?' matches any | |
178 | single character in a file name. | |
179 | .s3 | |
180 | Square brackets `[...]' specify | |
181 | a class of characters which | |
182 | matches any single file-name character in the class. | |
183 | Within the brackets, | |
184 | each ordinary character is taken | |
185 | to be a member of the class. | |
186 | A pair of characters separated by `\*-' places | |
187 | in the class | |
188 | each character lexically greater than or equal to | |
189 | the first and less than or equal to the second | |
190 | member of the pair. | |
191 | .s3 | |
192 | Other characters match only the same character in | |
193 | the file name. | |
194 | .s3 | |
195 | For example, `*' matches all file names; | |
196 | `?' matches all one-character file names; `[ab]*.s' matches | |
197 | all file names beginning with `a' or `b' and ending with `.s'; | |
198 | `?[zi\*-m]' matches all two-character file names ending | |
199 | with `z' or the letters `i' through `m'. | |
200 | .s3 | |
201 | If the argument with `*', `?', or `[' also contains a `/', a slightly | |
202 | different procedure is used: instead of the current directory, | |
203 | the directory used is the one obtained | |
204 | by taking the argument up to the last `/' before a `*' or `?'. | |
205 | If a `/' appears thereafter, then the intervening string is matched | |
206 | against the subdirectories of the derived directory. If there is no | |
207 | following `/', | |
208 | the matching process matches the remainder of the argument | |
209 | after this `/' against the files in the derived directory. | |
210 | For example: `/usr/dmr/a*.s' matches | |
211 | all files in directory `/usr/dmr' which begin | |
212 | with `a' and end with `.s'; | |
213 | `/mnt*/*/.q' matches e.g. `/mnt/ken/.q' and `/mnt/chuck/.q' among | |
214 | others. | |
215 | .s3 | |
216 | In any event, a list of names is obtained which match | |
217 | the argument. | |
218 | This list is sorted into alphabetical order, | |
219 | and the resulting sequence of arguments replaces the | |
220 | single argument containing the `*', `[', or `?'. | |
221 | The same process is carried out for each argument | |
222 | (the resulting lists are | |
223 | .it not | |
224 | merged) | |
225 | and finally the command is called with the resulting list of | |
226 | arguments. | |
227 | .s3 | |
228 | .bd "Quoting." | |
229 | The character `\\' causes the immediately following character | |
230 | to lose any special meaning it may have to the Shell; in this | |
231 | way `<', `>', and other characters meaningful to the | |
232 | Shell may be passed as part of arguments. | |
233 | A special case of this feature allows the continuation of commands | |
234 | onto more than one line: a new-line preceded by `\\' is translated | |
235 | into a blank. | |
236 | .s3 | |
237 | Sequences of characters enclosed in double (") or single (\*a) | |
238 | quotes are also taken literally. | |
239 | For example: | |
240 | .s3 | |
241 | ls | pr \*-h "My directory" | |
242 | .s3 | |
243 | causes a directory listing to be produced | |
244 | by | |
245 | .it ls, | |
246 | and passed on to | |
247 | .it pr | |
248 | to be | |
249 | printed with the heading `My directory'. | |
250 | Quotes permit the inclusion of blanks in | |
251 | the heading, which is a single argument to | |
252 | .it pr. | |
253 | .s3 | |
254 | .bd "Argument passing." | |
255 | When the Shell is invoked as a command, it has additional | |
256 | string processing capabilities. | |
257 | Recall that the form in which the Shell is invoked is | |
258 | .s3 | |
259 | sh [ name [ arg1 ... [ arg9 ... ] ] ] | |
260 | .s3 | |
261 | The | |
262 | .it name | |
263 | is the name of a file which is read and | |
264 | interpreted. | |
265 | If not given, this subinstance of the Shell | |
266 | continues to read the standard input file. | |
267 | .s3 | |
268 | In command lines in the file | |
269 | (not in command input), | |
270 | character sequences of the form `$n', where | |
271 | .it n | |
272 | is a digit, | |
273 | are replaced by the | |
274 | \fIn\fRth argument to the invocation | |
275 | of the Shell (argn). | |
276 | `$0' is replaced by | |
277 | .it name. | |
278 | Finally `$*' is replaced by the remaining arguments | |
279 | starting with `$1'. | |
280 | .s3 | |
281 | The argument `\*-t,' causes | |
282 | .it sh | |
283 | to read the standard input for a single line, execute | |
284 | it as a command, and then exit. | |
285 | This facility replaces the older `mini-shell.' | |
286 | It is useful for interactive programs | |
287 | which allow users to execute | |
288 | system commands. | |
289 | .s3 | |
290 | The argument `\*-c' (used with one following argument) | |
291 | causes the next argument to be taken as a command | |
292 | line and executed. | |
293 | No new-line need be present, but new-line characters | |
294 | are treated appropriately. | |
295 | This facility is useful as an alternative to | |
296 | `-t' where the caller has already read | |
297 | some of the characters of the command to be executed. | |
298 | .s3 | |
299 | The argument `\*-i' causes the shell to be `interactive', | |
300 | that is, the shell ignores interrupts and quits. | |
301 | Login shells are interactive, as are | |
302 | all shells whose input and output are both ttys. | |
303 | Interactive shells also have interruptible waits (see wait(I)). | |
304 | .s2 | |
305 | The argument `-v' causes the shell to echo each command line, with | |
306 | a prompt, as it is printed. | |
307 | .s3 | |
308 | .bd "End of file." | |
309 | An end-of-file in the Shell's input causes it to exit. | |
310 | A side effect of this fact means that the way to | |
311 | log out from UNIX is to type an EOT. | |
312 | .s3 | |
313 | .bd "Special commands." | |
314 | The following commands are treated specially by the Shell. | |
315 | .s3 | |
316 | .it chdir | |
317 | is done without | |
318 | spawning a new process by executing | |
319 | .it "sys chdir" | |
320 | (II). | |
321 | "cd" is a useful abbreviation for "chdir". | |
322 | .s3 | |
323 | .it login | |
324 | is done by executing | |
325 | /bin/login without creating a new process. | |
326 | .s3 | |
327 | .it wait | |
328 | is done without spawning a new process by | |
329 | executing | |
330 | .it "sys wait" | |
331 | (II). | |
332 | .s3 | |
333 | .it shift | |
334 | is done by manipulating the arguments | |
335 | to the Shell. | |
336 | .s3 | |
337 | .it lib | |
338 | is done by manipulating internal data. | |
339 | .s3 | |
340 | `\fB:\fR' is simply ignored. | |
341 | .s3 | |
342 | .bd "Command file errors; interrupts." | |
343 | Any Shell-detected error, or an interrupt signal, | |
344 | during the execution of a command file | |
345 | causes the Shell to cease execution of that file. | |
346 | .s3 | |
347 | Processes that are created with `&' ignore interrupts. | |
348 | Also if such a process has not redirected its | |
349 | input with a `<', | |
350 | its input is automatically redirected to the | |
351 | zero length file /dev/null. | |
352 | .sh FILES | |
353 | /etc/glob2, | |
354 | which interprets `*', `?', and `['. | |
355 | .br | |
356 | /dev/null as a source of end-of-file. | |
357 | .br | |
358 | /bin/px the Pascal interpreter. | |
359 | .sh "SEE ALSO" | |
360 | `The UNIX Time-Sharing System', | |
361 | CACM, July, 1974. | |
362 | .s2 | |
363 | chdir (I), login (I), wait (I), shift (I), glob (VIII), cd (I), lib (I) |