Commit | Line | Data |
---|---|---|
af3e82f7 WJ |
1 | .\" Copyright (c) 1991 The Regents of the University of California. |
2 | .\" All rights reserved. | |
3 | .\" | |
4 | .\" This code is derived from software contributed to Berkeley by | |
5 | .\" Kenneth Almquist. | |
6 | .\" | |
7 | .\" Redistribution and use in source and binary forms, with or without | |
8 | .\" modification, are permitted provided that the following conditions | |
9 | .\" are met: | |
10 | .\" 1. Redistributions of source code must retain the above copyright | |
11 | .\" notice, this list of conditions and the following disclaimer. | |
12 | .\" 2. Redistributions in binary form must reproduce the above copyright | |
13 | .\" notice, this list of conditions and the following disclaimer in the | |
14 | .\" documentation and/or other materials provided with the distribution. | |
15 | .\" 3. All advertising materials mentioning features or use of this software | |
16 | .\" must display the following acknowledgement: | |
17 | .\" This product includes software developed by the University of | |
18 | .\" California, Berkeley and its contributors. | |
19 | .\" 4. Neither the name of the University nor the names of its contributors | |
20 | .\" may be used to endorse or promote products derived from this software | |
21 | .\" without specific prior written permission. | |
22 | .\" | |
23 | .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
24 | .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
25 | .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
26 | .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
27 | .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
28 | .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
29 | .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
30 | .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
31 | .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
32 | .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
33 | .\" SUCH DAMAGE. | |
34 | .\" | |
35 | .\" @(#)sh.1 5.1 (Berkeley) 3/7/91 | |
36 | .\" | |
37 | .TH SH 1 "March 7, 1991" | |
38 | .UC 7 | |
39 | .de h \" subheading | |
40 | .sp | |
41 | .ti -0.3i | |
42 | .B "\\$1" | |
43 | .PP | |
44 | .. | |
45 | .de d \" begin display | |
46 | .sp | |
47 | .in +4 | |
48 | .nf | |
49 | .. | |
50 | .de e \" end display | |
51 | .in -4 | |
52 | .fi | |
53 | .sp | |
54 | .. | |
55 | .de c \" command, etc. | |
56 | .br | |
57 | .HP 5 | |
58 | \fB\\$1\fR | |
59 | .br | |
60 | .. | |
61 | .de b \" begin builtin command | |
62 | .HP 5 | |
63 | .B \\$1 | |
64 | .. | |
65 | .SH NAME | |
66 | ash \- a shell | |
67 | .SH SYNOPSIS | |
68 | .B ash | |
69 | [ | |
70 | .B -efIijnsxz | |
71 | ] [ | |
72 | .B +efIijnsxz | |
73 | ] [ | |
74 | .B -c | |
75 | .I command | |
76 | ] [ | |
77 | .I arg | |
78 | ] ... | |
79 | .SH COPYRIGHT | |
80 | Copyright 1989 by Kenneth Almquist. | |
81 | .SH DESCRIPTION | |
82 | .I Ash | |
83 | is a version of | |
84 | .I sh | |
85 | with features similar to those of the System V shell. | |
86 | This manual page lists all the features of | |
87 | .I ash | |
88 | but concentrates on the ones not in other shells. | |
89 | .h "Invocation" | |
90 | If the | |
91 | .B -c | |
92 | options is given, then the shell executes the specified shell command. | |
93 | The | |
94 | .B -s | |
95 | flag cause the shell to read commands from the standard input (after | |
96 | executing any command specified with the | |
97 | .B -c | |
98 | option. | |
99 | If neither the | |
100 | .B -s | |
101 | or | |
102 | .B -c | |
103 | options are set, then the first | |
104 | .I arg | |
105 | is taken as the name of a file to read commands from. | |
106 | If this is impossible because there are no arguments following | |
107 | the options, then | |
108 | .I ash | |
109 | will set the | |
110 | .B -s | |
111 | flag and will read commands from the standard input. | |
112 | .PP | |
113 | The shell sets the initial value of the positional parameters from the | |
114 | .IR arg s | |
115 | remaining after any | |
116 | .I arg | |
117 | used as the name of a file of commands is deleted. | |
118 | .PP | |
119 | The flags (other than | |
120 | .BR -c ) | |
121 | are set by preceding them with ``-'' and cleared by preceding them | |
122 | with ``+''; see the | |
123 | .I set | |
124 | builtin command for a list of flags. | |
125 | If no value is specified for the | |
126 | .B -i | |
127 | flag, the | |
128 | .B -s | |
129 | flag is set, and the standard input and output of the shell | |
130 | are connected to terminals, then the | |
131 | .B -i | |
132 | flag will be set. | |
133 | If no value is specified for the | |
134 | .B -j | |
135 | flag, then the | |
136 | .B -j | |
137 | flag will be set if the | |
138 | .B -i | |
139 | flag is set. | |
140 | .PP | |
141 | When the shell is invoked with the | |
142 | .B -c | |
143 | option, it is good practice to include the | |
144 | .I -i | |
145 | flag if the command was entered interactively by a user. | |
146 | For compatibility with the System V shell, the | |
147 | .I -i | |
148 | option should come after the | |
149 | .B -c | |
150 | option. | |
151 | .PP | |
152 | If the first character of argument zero to the shell is ``-'', | |
153 | the shell is assumed to be a login shell, and the files | |
154 | .B /etc/profile | |
155 | and | |
156 | .B .profile | |
157 | are read if they exist. | |
158 | If the environment variable SHINIT is set on entry to the shell, | |
159 | the commands in SHINIT are normally parsed and executed. SHINIT is | |
160 | not examined if the shell is a login shell, or if it the shell is running a | |
161 | shell procedure. (A shell is considered to be running a shell | |
162 | procedure if neither the | |
163 | .B -s | |
164 | nor the | |
165 | .B -c | |
166 | options are set.) | |
167 | .h "Control Structures" | |
168 | A | |
169 | .I list | |
170 | is a sequence of zero or more commands separated by newlines, | |
171 | semicolons, or ampersands, and optionally terminated by one of these | |
172 | three characters. (This differs from the System V shell, which | |
173 | requires a list to contain at least one command in most cases.) The | |
174 | commands in a list are executed in the order they are written. | |
175 | If command is followed by an ampersand, the shell starts the command | |
176 | and immediately proceed onto the next command; otherwise it waits | |
177 | for the command to terminate before proceeding to the next one. | |
178 | .PP | |
179 | ``&&'' and ``||'' are binary operators. | |
180 | ``&&'' executes the first command, and then executes the second command | |
181 | iff the exit status of the first command is zero. ``||'' is similar, | |
182 | but executes the second command iff the exit status of the first command | |
183 | is nonzero. ``&&'' and ``||'' both have the same priority. | |
184 | .PP | |
185 | The ``|'' operator is a binary operator which feeds the standard output | |
186 | of the first command into the standard input of the second command. | |
187 | The exit status of the ``|'' operator is the exit status of the second | |
188 | command. ``|'' has a higher priority than ``||'' or ``&&''. | |
189 | .PP | |
190 | An | |
191 | .I if | |
192 | command looks like | |
193 | .d | |
194 | \fBif\fR list | |
195 | \fBthen\fR list | |
196 | .ti -\w'[ 'u | |
197 | [ \fBelif\fR list | |
198 | \fBthen\fR list ] ... | |
199 | .ti -\w'[ 'u | |
200 | [ \fBelse\fR list ] | |
201 | \fBfi\fR | |
202 | .e | |
203 | .PP | |
204 | A | |
205 | .I while | |
206 | command looks like | |
207 | .d | |
208 | \fBwhile\fR list | |
209 | \fBdo\fR list | |
210 | \fBdone\fR | |
211 | .e | |
212 | The two lists are executed repeatedly while the exit status of the first | |
213 | list is zero. The | |
214 | .I until | |
215 | command is similar, but has the word | |
216 | .B until | |
217 | in place of | |
218 | .B while | |
219 | repeats until the exit status of the first list | |
220 | is zero. | |
221 | .PP | |
222 | The | |
223 | .I for | |
224 | command looks like | |
225 | .d | |
226 | \fBfor\fR variable \fBin\fR word... | |
227 | \fBdo\fR list | |
228 | \fBdone\fR | |
229 | .e | |
230 | The words are expanded, and then the list is executed repeatedly with | |
231 | the variable set to each word in turn. | |
232 | .B do | |
233 | and | |
234 | .B done | |
235 | may be replaced with | |
236 | ``{'' and ``}''. | |
237 | .PP | |
238 | The | |
239 | .I break | |
240 | and | |
241 | .I continue | |
242 | commands look like | |
243 | .d | |
244 | \fBbreak\fR [ num ] | |
245 | \fBcontinue\fR [ num ] | |
246 | .e | |
247 | .I Break | |
248 | terminates the | |
249 | .I num | |
250 | innermost | |
251 | .I for | |
252 | or | |
253 | .I while | |
254 | loops. | |
255 | .I Continue | |
256 | continues with the next iteration of the | |
257 | .IRnum'th | |
258 | innermost loop. | |
259 | These are implemented as builtin commands. | |
260 | .PP | |
261 | The | |
262 | .I case | |
263 | command looks like | |
264 | .d | |
265 | \fBcase\fR word \fBin\fR | |
266 | pattern\fB)\fR list \fB;;\fR | |
267 | \&... | |
268 | \fBesac\fR | |
269 | .e | |
270 | The pattern can actually be one or more patterns (see | |
271 | .I Patterns | |
272 | below), separated by ``|'' characters. | |
273 | .PP | |
274 | Commands may be grouped by writing either | |
275 | .d | |
276 | \fB(\fRlist\fB)\fR | |
277 | .e | |
278 | or | |
279 | .d | |
280 | \fB{\fR list; \fB}\fR | |
281 | .e | |
282 | The first of these executes the commands in a subshell. | |
283 | .PP | |
284 | A function definition looks like | |
285 | .d | |
286 | name \fB( )\fR command | |
287 | .e | |
288 | A function definition is an executable statement; when executed it installs | |
289 | a function named | |
290 | .B name | |
291 | and returns an exit status of zero. | |
292 | The command is normally a list enclosed between ``{'' and ``}''. | |
293 | .PP | |
294 | Variables may be declared to be local to a function by using a | |
295 | .I local | |
296 | command. This should appear as the first staement of a function, | |
297 | and looks like | |
298 | .d | |
299 | \fBlocal\fR [ variable | \fB-\fR ] ... | |
300 | .e | |
301 | .I Local | |
302 | is implemented as a builtin command. | |
303 | .PP | |
304 | When a variable is made local, it inherits the initial value and | |
305 | exported and readonly flags from the variable with the same name in the | |
306 | surrounding scope, if there is one. Otherwise, the variable is | |
307 | initially unset. | |
308 | .I Ash | |
309 | uses dynamic scoping, so that if you make the variable | |
310 | .B x | |
311 | local to function | |
312 | .IR f , | |
313 | which then calls function | |
314 | .IR g , | |
315 | references to the variable | |
316 | .B x | |
317 | made inside | |
318 | .I g | |
319 | will refer to the variable | |
320 | .B x | |
321 | declared inside | |
322 | .IR f , | |
323 | not to the global variable named | |
324 | .BR x . | |
325 | .PP | |
326 | The only special parameter than can be made local is ``\fB-\fR''. | |
327 | Making ``\fB-\fR'' local any shell options that are changed via the | |
328 | .I set | |
329 | command inside the function to be restored to their original values | |
330 | when the function returns. | |
331 | .PP | |
332 | The | |
333 | .I return | |
334 | command looks like | |
335 | .d | |
336 | \fBreturn\fR [ exitstatus ] | |
337 | .e | |
338 | It terminates the currently executing function. | |
339 | .I Return | |
340 | is implemented as a builtin command. | |
341 | .h "Simple Commands" | |
342 | A simple command is a sequence of words. The execution of a simple | |
343 | command proceeds as follows. First, the leading words of the form | |
344 | ``name=value'' are stripped off and assigned to the environment of | |
345 | the command. Second, the words are expanded. Third, the first | |
346 | remaining word is taken as the command name that command is located. | |
347 | Fourth, any redirections are performed. Fifth, the command is | |
348 | executed. We look at these operations in reverse order. | |
349 | .PP | |
350 | The execution of the command varies with the type of command. | |
351 | There are three types of commands: shell functions, builtin commands, | |
352 | and normal programs. | |
353 | .PP | |
354 | When a shell function is executed, all of the shell positional parameters | |
355 | (except $0, which remains unchanged) are set to the parameters to the shell | |
356 | function. The variables which are explicitly placed in the environment | |
357 | of the command (by placing assignments to them before the function name) | |
358 | are made local to the function and are set to values given. | |
359 | Then the command given in the function definition is executed. | |
360 | The positional parameters are restored to their original values when | |
361 | the command completes. | |
362 | .PP | |
363 | Shell builtins are executed internally to the shell, without spawning | |
364 | a new process. | |
365 | .PP | |
366 | When a normal program is executed, the shell runs the program, passing | |
367 | the parameters and the environment to the program. If the program is | |
368 | a shell procedure, the shell will interpret the program in a subshell. | |
369 | The shell will reinitialize itself in this case, so that the effect | |
370 | will be as if a new shell had been invoked to handle the shell procedure, | |
371 | except that the location of commands located in the parent shell will | |
372 | be remembered by the child. If the program is a file beginning with | |
373 | ``#!'', the remainder of the first line specifies an interpreter for | |
374 | the program. The shell (or the operating system, under Berkeley UNIX) | |
375 | will run the interpreter in this case. The arguments to the interpreter | |
376 | will consist of any arguments given on the first line of the program, | |
377 | followed by the name of the program, followed by the arguments passed | |
378 | to the program. | |
379 | .h "Redirection" | |
380 | Input/output redirections can be intermixed with the words in a simple | |
381 | command and can be placed following any of the other commands. When | |
382 | redirection occurs, the shell saves the old values of the file descriptors | |
383 | and restores them when the command completes. The ``<'', ``>'', and ``>>'' | |
384 | redirections open a file for input, output, and appending, respectively. | |
385 | The ``<&digit'' and ``>&digit'' makes the input or output a duplicate | |
386 | of the file descriptor numbered by the digit. If a minus sign is used | |
387 | in place of a digit, the standard input or standard output are closed. | |
388 | .PP | |
389 | The ``<<\ word'' redirection | |
390 | takes input from a | |
391 | .I here | |
392 | document. | |
393 | As the shell encounters ``<<'' redirections, it collects them. The | |
394 | next time it encounters an unescaped newline, it reads the documents | |
395 | in turn. The word following the ``<<'' specifies the contents of the | |
396 | line that terminates the document. If none of the quoting methods | |
397 | ('', "", or \e) are used to enter the word, then the document is treated | |
398 | like a word inside double quotes: ``$'' and backquote are expanded | |
399 | and backslash can be used to escape these and to continue long lines. | |
400 | The word cannot contain any variable or command substitutions, and | |
401 | its length (after quoting) must be in the range of 1 to 79 characters. | |
402 | If ``<<-'' is used in place of ``<<'', then leading tabs are deleted | |
403 | from the lines of the document. (This is to allow you do indent shell | |
404 | procedures containing here documents in a natural fashion.) | |
405 | .PP | |
406 | Any of the preceding redirection operators may be preceded by a single | |
407 | digit specifying the file descriptor to be redirected. There cannot | |
408 | be any white space between the digit and the redirection operator. | |
409 | .h "Path Search" | |
410 | When locating a command, the shell first looks to see if it has a | |
411 | shell function by that name. Then, if PATH does not contain an | |
412 | entry for "%builtin", it looks for a builtin command by that name. | |
413 | Finally, it searches each entry in PATH in turn for the command. | |
414 | .PP | |
415 | The value of the PATH variable should be a series of entries separated | |
416 | by colons. | |
417 | Each entry consists of a directory name, or a directory name followed | |
418 | by a flag beginning with a percent sign. | |
419 | The current directory should be indicated by an empty directory name. | |
420 | .PP | |
421 | If no percent sign is present, then the entry causes the shell to | |
422 | search for the command in the specified directory. If the flag is | |
423 | ``%builtin'' then the list of shell builtin commands is searched. | |
424 | If the flag is ``%func'' then the directory is searched for a file which | |
425 | is read as input to the shell. This file should define a function | |
426 | whose name is the name of the command being searched for. | |
427 | .PP | |
428 | Command names containing a slash are simply executed without performing | |
429 | any of the above searches. | |
430 | .h "The Environment" | |
431 | The environment of a command is a set of name/value pairs. When the | |
432 | shell is invoked, it reads these names and values, sets the shell | |
433 | variables with these names to the corresponding values, and marks | |
434 | the variables as exported. The | |
435 | .I export | |
436 | command can be used to mark additional variables as exported. | |
437 | .PP | |
438 | The environment of a command is constructed by constructing name/value | |
439 | pairs from all the exported shell variables, and then modifying this | |
440 | set by the assignments which precede the command, if any. | |
441 | .h "Expansion" | |
442 | The process of evaluating words when a shell procedure is executed is | |
443 | called | |
444 | .IR expansion . | |
445 | Expansion consists of four steps: variable substitution, command | |
446 | substitution, word splitting, and file name generation. If a word | |
447 | is the expression following the word | |
448 | .B case | |
449 | in a case statement, the file name | |
450 | which follows a redirection symbol, or an assignment to the environment | |
451 | of a command, then the word cannot be split into multiple words. In | |
452 | these cases, the last two steps of the expansion process are omitted. | |
453 | .h "Variable Substitution" | |
454 | To be written. | |
455 | .h "Command Substitution" | |
456 | .I Ash | |
457 | accepts two syntaxes for command substitution: | |
458 | .b | |
459 | `\fIlist\fR` | |
460 | .e | |
461 | and | |
462 | .b | |
463 | $(\fIlist\fR) | |
464 | .e | |
465 | Either of these may be included in a word. | |
466 | During the command substitution process, the command (syntactly a | |
467 | .IR list ) | |
468 | will be executed and anything that the command writes to the standard | |
469 | output will be captured by the shell. The final newline (if any) of | |
470 | the output will be deleted; the rest of the output will be substituted | |
471 | for the command in the word. | |
472 | .h "Word Splitting" | |
473 | When the value of a variable or the output of a command is substituted, | |
474 | the resulting text is subject to word splitting, unless the dollar sign | |
475 | introducing the variable or backquotes containing the text were enclosed | |
476 | in double quotes. In addition, ``$@'' is subject to a special type of | |
477 | splitting, even in the presence of double quotes. | |
478 | .PP | |
479 | Ash uses two different splitting algorithms. The normal approach, which | |
480 | is intended for splitting text separated by which space, is used if the | |
481 | first character of the shell variable IFS is a space. Otherwise an alternative | |
482 | experimental algorithm, which is useful for splitting (possibly empty) | |
483 | fields separated by a separator character, is used. | |
484 | .PP | |
485 | When performing splitting, the shell scans the replacement text looking | |
486 | for a character (when IFS does not begin with a space) or a sequence of | |
487 | characters (when IFS does begin with a space), deletes the character or | |
488 | sequence of characters, and spits the word into two strings at that | |
489 | point. When IFS begins with a space, the shell deletes either of the | |
490 | strings if they are null. As a special case, if the word containing | |
491 | the replacement text is the null string, the word is deleted. | |
492 | .PP | |
493 | The variable ``$@'' is special in two ways. First, splitting takes | |
494 | place between the positional parameters, even if the text is enclosed | |
495 | in double quotes. Second, if the word containing the replacement | |
496 | text is the null string and there are no positional parameters, then | |
497 | the word is deleted. The result of these rules is that "$@" is | |
498 | equivalent to "$1" "$2" ... "$\fIn\fR", where \fIn\fR is the number of | |
499 | positional parameters. (Note that this differs from the System V shell. | |
500 | The System V documentation claims that "$@" behaves this way; in fact | |
501 | on the System V shell "$@" is equivalent to "" when there are no | |
502 | positional paramteters.) | |
503 | .h "File Name Generation" | |
504 | Unless the | |
505 | .B -f | |
506 | flag is set, file name generation is performed after word splitting is | |
507 | complete. Each word is viewed as a series of patterns, separated by | |
508 | slashes. The process of expansion replaces the word with the names of | |
509 | all existing files whose names can be formed by replacing each pattern | |
510 | with a string that matches the specified pattern. There are two | |
511 | restrictions on this: first, a pattern cannot match a string containing | |
512 | a slash, and second, a pattern cannot match a string starting with a | |
513 | period unless the first character of the pattern is a period. | |
514 | .PP | |
515 | If a word fails to match any files and the | |
516 | .B -z | |
517 | flag is not set, then the word will be left unchanged (except that the | |
518 | meta-characters will be converted to normal characters). If the | |
519 | .B -z | |
520 | flag is set, then the word is only left unchanged if none | |
521 | of the patterns contain a character that can match anything besides | |
522 | itself. Otherwise the | |
523 | .B -z | |
524 | flag forces the word to be replaced with the names of the files that it | |
525 | matches, even if there are zero names. | |
526 | .h "Patterns" | |
527 | A | |
528 | .I pattern | |
529 | consists of normal characters, which match themselves, and meta-characters. | |
530 | The meta-characters are ``!'', ``*'', ``?'', and ``[''. These characters lose | |
531 | there special meanings if they are quoted. When command or variable | |
532 | substitution is performed and the dollar sign or back quotes are not | |
533 | double quoted, the value of the variable or the output of the command | |
534 | is scanned for these characters and they are turned into meta-characters. | |
535 | .PP | |
536 | Two exclamation points at the beginning of a pattern function as a ``not'' | |
537 | operator, causing the pattern to match any string that the remainder of | |
538 | the pattern does | |
539 | .I not | |
540 | match. Other occurances of exclamation points in a pattern match | |
541 | exclamation points. Two exclamation points are required rather than one | |
542 | to decrease the incompatibility with the System V shell (which does not | |
543 | treat exclamation points specially). | |
544 | .PP | |
545 | An asterisk (``*'') matches any string of characters. | |
546 | A question mark matches any single character. | |
547 | A left bracket (``['') introduces a character class. The end of the | |
548 | character class is indicated by a ``]''; if the ``]'' is missing then | |
549 | the ``['' matches a ``['' rather than introducing a character class. | |
550 | A character class matches any of the characters between the square | |
551 | brackets. A range of characters may be specified using a minus sign. | |
552 | The character class may be complemented by making an exclamation point | |
553 | the first character of the character class. | |
554 | .PP | |
555 | To include a ``]'' in a character class, make it the first character listed | |
556 | (after the ``!'', if any). | |
557 | To include a minus sign, make it the first or last character listed. | |
558 | .h "The /u Directory" | |
559 | By convention, the name ``/u/user'' refers to the home directory of the | |
560 | specified user. There are good reasons why this feature should be supported | |
561 | by the file system (using a feature such as symbolic links) rather than | |
562 | by the shell, but | |
563 | .I ash | |
564 | is capable of performing this mapping if the file system doesn't. | |
565 | If the mapping is done by | |
566 | .IR ash , | |
567 | setting the | |
568 | .B -f | |
569 | flag will turn it off. | |
570 | .h "Character Set" | |
571 | .I Ash | |
572 | silently discards nul characters. Any other character will be handled | |
573 | correctly by | |
574 | .IR ash , | |
575 | including characters with the high order bit set. | |
576 | .h "Job Names and Job Control" | |
577 | The term | |
578 | .I job | |
579 | refers to a process created by a shell command, or in the case of a | |
580 | pipeline, to the set of processes in the pipeline. The ways to refer | |
581 | to a job are: | |
582 | .b | |
583 | %\fInumber\fR | |
584 | %\fIstring\fR | |
585 | %% | |
586 | \fIprocess_id\fR | |
587 | .e | |
588 | The first form identifies a job by job number. | |
589 | When a command is run, | |
590 | .I ash | |
591 | assigns it a job number | |
592 | (the lowest unused number is assigned). | |
593 | The second form identifies a job by giving a prefix of the command used | |
594 | to create the job. The prefix must be unique. If there is only one job, | |
595 | then the null prefix will identify the job, so you can refer to the job | |
596 | by writing ``%''. The third form refers to the \fIcurrent job\fR. The | |
597 | current job is the last job to be stopped while it was in the foreground. | |
598 | (See the next paragraph.) The last form identifies a job by giving the | |
599 | process id of the last process in the job. | |
600 | .PP | |
601 | If the operating system that | |
602 | .I ash | |
603 | is running on supports job control, | |
604 | .I ash | |
605 | will allow you to use it. | |
606 | In this case, typing the suspend character (typically ^Z) while running | |
607 | a command will return you to | |
608 | .I ash | |
609 | and will make the suspended command the current job. You can then continue | |
610 | the job in the background by typing | |
611 | .IR bg , | |
612 | or you can continue it in the foreground by typing | |
613 | .IR fg . | |
614 | .h "Atty" | |
615 | If the shell variable ATTY is set, and the shell variable TERM is not | |
616 | set to ``emacs'', then \fIash\fR generates appropriate escape sequences | |
617 | to talk to | |
618 | .IR atty (1). | |
619 | .h "Exit Statuses" | |
620 | By tradition, an exit status of zero means that a command has succeeded | |
621 | and a nonzero exit status indicates that the command failed. This is | |
622 | better than no convention at all, but in practice it is extremely useful | |
623 | to allow commands that succeed to use the exit status to return information | |
624 | to the caller. A variety of better conventions have been proposed, but | |
625 | none of them has met with universal approval. The convention used by | |
626 | \fIash\fR and all the programs included in the \fIash\fR distribution is | |
627 | as follows: | |
628 | .ta 1i,2i | |
629 | .nf | |
630 | 0 Success. | |
631 | 1 Alternate success. | |
632 | 2 Failure. | |
633 | 129-... Command terminated by a signal. | |
634 | .fi | |
635 | The \fIalternate success\fR return is used by commands to indicate various | |
636 | conditions which are not errors but which can, with a little imagination, | |
637 | be conceived of as less successful than plain success. For example, | |
638 | .I test | |
639 | returns 1 when the tested condition is false and | |
640 | .I getopts | |
641 | returns 1 when there are no more options. | |
642 | Because this convention is not used universally, the | |
643 | .B -e | |
644 | option of | |
645 | .I ash | |
646 | causes the shell to exit when a command returns 1 even though that | |
647 | contradicts the convention described here. | |
648 | .PP | |
649 | When a command is terminated by a signal, the uses 128 plus the signal | |
650 | number as the exit code for the command. | |
651 | .h "Builtin Commands" | |
652 | This concluding section lists the builtin commands which are builtin | |
653 | because they need to perform some operation that can't be performed by a | |
654 | separate process. In addition to these, there are several other commands | |
655 | .RI ( catf , | |
656 | .IR echo , | |
657 | .IR expr , | |
658 | .IR line , | |
659 | .IR nlecho , | |
660 | .IR test , | |
661 | .RI `` : '', | |
662 | and | |
663 | .IR true ) | |
664 | which can optionally be compiled into the shell. The builtin | |
665 | commands described below that accept options use the System V Release 2 | |
666 | .IR getopt (3) | |
667 | syntax. | |
668 | .sp | |
669 | .b bg | |
670 | [ | |
671 | .I job | |
672 | ] ... | |
673 | .br | |
674 | Continue the specified jobs (or the current job if no jobs are given) | |
675 | in the background. | |
676 | This command is only available on systems with Bekeley job control. | |
677 | .b bltin | |
678 | .IR "command arg" ... | |
679 | .br | |
680 | Execute the specified builtin command. (This is useful when you have a | |
681 | shell function with the same name as a builtin command.) | |
682 | .b cd | |
683 | [ | |
684 | .I directory | |
685 | ] | |
686 | .br | |
687 | Switch to the specified directory (default $HOME). | |
688 | If the an entry for CDPATH appears in the environment of the cd command | |
689 | or the shell variable CDPATH is set and the directory name does not | |
690 | begin with a slash, then the directories listed in CDPATH will be | |
691 | searched for the specified directory. The format of CDPATH is the | |
692 | same as that of PATH. | |
693 | In an interactive shell, the cd command will print out the name of the | |
694 | directory that it actually switched to if this is different from the | |
695 | name that the user gave. These may be different either because | |
696 | the CDPATH mechanism was used or because a symbolic link was crossed. | |
697 | .b ".\fI\h'0.1i'file" | |
698 | .br | |
699 | The commands in the specified file are read and executed by the shell. | |
700 | A path search is not done to find the file because the directories in | |
701 | PATH generally contain files that are intended to be executed, not read. | |
702 | .b eval | |
703 | .IR string ... | |
704 | .br | |
705 | The strings are parsed as shell commands and executed. | |
706 | (This differs from the System V shell, which concatenates the arguments | |
707 | (separated by spaces) and parses the result as a single command.) | |
708 | .b exec | |
709 | [ | |
710 | .IR "command arg" ... | |
711 | ] | |
712 | .br | |
713 | Unless | |
714 | .I command | |
715 | is omitted, | |
716 | the shell process is replaced with the specified program (which must be a real | |
717 | program, not a shell builtin or function). | |
718 | Any redirections on the exec command are marked as permanent, so that they | |
719 | are not undone when the exec command finishes. | |
720 | If the command is not found, the exec command causes the shell to exit. | |
721 | .b exit | |
722 | [ | |
723 | .I exitstatus | |
724 | ] | |
725 | .br | |
726 | Terminate the shell process. If | |
727 | .I exitstatus | |
728 | is given it is used as the | |
729 | exit status of the shell; otherwise the exit status of the preceding | |
730 | command is used. | |
731 | .b export | |
732 | .IR name ... | |
733 | .br | |
734 | The specified names are exported so that they will appear in the environment | |
735 | of subsequent commands. The only way to un-export a variable is to unset it. | |
736 | .I Ash | |
737 | allows the value of a variable to be set at the same time it is exported | |
738 | by writing | |
739 | .d | |
740 | \fBexport\fR name=value | |
741 | .e | |
742 | With no arguments the export command lists the names of all exported variables. | |
743 | .b fg | |
744 | [ | |
745 | .I job | |
746 | ] | |
747 | .br | |
748 | Move the specified job or the current job to the foreground. | |
749 | This command is only available on systems with Bekeley job control. | |
750 | .b getopts | |
751 | .I optstring | |
752 | .I var | |
753 | .br | |
754 | The System V | |
755 | .I getopts | |
756 | command. | |
757 | .b hash | |
758 | .B -rv | |
759 | .IR command ... | |
760 | .br | |
761 | The shell maintains a hash table which remembers the locations of | |
762 | commands. With no arguments whatsoever, the hash command prints | |
763 | out the contents of this table. Entries which have not been looked | |
764 | at since the last | |
765 | .I cd | |
766 | command are marked with an asterisk; it is possible for these entries | |
767 | to be invalid. | |
768 | .sp | |
769 | With arguments, the hash command removes the specified commands from | |
770 | the hash table (unless they are functions) and then locates them. | |
771 | With the | |
772 | .B -v | |
773 | option, | |
774 | .I hash | |
775 | prints the locations of the commands as it finds them. | |
776 | The | |
777 | .B -r | |
778 | option causes the | |
779 | .I hash | |
780 | command to delete all the entries in the hash table except for | |
781 | functions. | |
782 | .b jobid | |
783 | [ | |
784 | .I job | |
785 | ] | |
786 | .br | |
787 | Print the process id's of the processes in the job. If the job argument | |
788 | is omitted, use the current job. | |
789 | .b jobs | |
790 | .br | |
791 | This command lists out all the background processes which are children | |
792 | of the current shell process. | |
793 | .b lc | |
794 | [ | |
795 | .I function-name | |
796 | ] | |
797 | .br | |
798 | The function name is defined to execute the last command entered. | |
799 | If the function name is omitted, the last command executed is | |
800 | executed again. This command only works if the | |
801 | .B -i | |
802 | flag is set. | |
803 | .b pwd | |
804 | .br | |
805 | Print the current directory. The builtin command may differ from the | |
806 | program of the same name because the builtin command remembers what | |
807 | the current directory is rather than recomputing it each time. This | |
808 | makes it faster. However, if the current directory is renamed, the | |
809 | builtin version of pwd will continue to print the old name for the | |
810 | directory. | |
811 | .b read | |
812 | [ | |
813 | .B -p | |
814 | .I prompt | |
815 | ] | |
816 | [ | |
817 | .B -e | |
818 | ] | |
819 | .IR variable ... | |
820 | .br | |
821 | The prompt is printed if the | |
822 | .B -p | |
823 | option is specified and the standard input is a terminal. Then a | |
824 | line is read from the standard input. The trailing newline is deleted | |
825 | from the line and the line is split as described | |
826 | in the section on word splitting above, and the pieces are assigned to | |
827 | the variables in order. If there are more pieces than variables, the | |
828 | remaining pieces (along with the characters in IFS that separated them) | |
829 | are assigned to the last variable. If there are more variables than | |
830 | pieces, the remaining variables are assigned the null string. | |
831 | .sp | |
832 | The | |
833 | .B -e | |
834 | option causes any backslashes in the input to be treated specially. | |
835 | If a backslash is followed by a newline, the backslash and the newline | |
836 | will be deleted. If a backslash is followed by any other character, | |
837 | the backslash will be deleted and the following character will be treated | |
838 | as though it were not in IFS, even if it is. | |
839 | .b readonly | |
840 | .IR name ... | |
841 | .br | |
842 | The specified names are marked as read only, so that they cannot be | |
843 | subsequently modified or unset. | |
844 | .I Ash | |
845 | allows the value of a variable to be set at the same time it is marked | |
846 | read only by writing | |
847 | .d | |
848 | \fBreadonly\fR name=value | |
849 | .e | |
850 | With no arguments the readonly command lists the names of all | |
851 | read only variables. | |
852 | .b set | |
853 | [ | |
854 | { | |
855 | .BI - options | |
856 | | | |
857 | .BI + options | |
858 | | | |
859 | .B -- | |
860 | } | |
861 | ] | |
862 | .IR arg ... | |
863 | .br | |
864 | The | |
865 | .I set | |
866 | command performs three different functions. | |
867 | .sp | |
868 | With no arguments, it lists the values of all shell variables. | |
869 | .sp | |
870 | If options are given, it sets the specified option flags, or clears | |
871 | them if the option flags are introduced with a | |
872 | .B + | |
873 | rather than a | |
874 | .BR - . | |
875 | Only the first argument to | |
876 | .I set | |
877 | can contain options. | |
878 | The possible options are: | |
879 | .sp | |
880 | .ta 0.4i | |
881 | .in +0.4i | |
882 | .ti -0.4i | |
883 | \fB-e\fR Causes the shell to exit when a command terminates with | |
884 | a nonzero exit status, except when the exit status of the command is | |
885 | explicitly tested. The exit status of a command is considered to be | |
886 | explicitly tested if the command is used to control an | |
887 | .IR if , | |
888 | .IR elif , | |
889 | .IR while , | |
890 | or | |
891 | .IR until ; | |
892 | or if the command is the left hand operand of an ``&&'' or ``||'' | |
893 | operator. | |
894 | .sp | |
895 | .ti -0.4i | |
896 | \fB-f\fR Turn off file name generation. | |
897 | .sp | |
898 | .ti -0.4i | |
899 | \fB-I\fR Cause the shell to ignore end of file conditions. | |
900 | (This doesn't apply when the shell a script sourced using the ``.'' | |
901 | command.) The shell will in fact exit if it gets 50 eof's in a | |
902 | row. | |
903 | .sp | |
904 | .ti -0.4i | |
905 | \fB-i\fR Make the shell interactive. This causes the shell to | |
906 | prompt for input, to trap interrupts, to ignore quit and terminate signals, | |
907 | and to return to the main command loop rather than exiting on error. | |
908 | .sp | |
909 | .ti -0.4i | |
910 | \fB-j\fR Turns on Berkeley job control, on systems that support it. | |
911 | When the shell starts up, the | |
912 | .B -j | |
913 | is set by default if the | |
914 | .B -i | |
915 | flag is set. | |
916 | .sp | |
917 | .ti -0.4i | |
918 | \fB-n\fR Causes the shell to read commands but not execute them. | |
919 | (This is marginally useful for checking the syntax of scripts.) | |
920 | .sp | |
921 | .ti -0.4i | |
922 | \fB-s\fR If this flag is set when the shell starts up, the shell | |
923 | reads commands from its standard input. The shell doesn't examine the | |
924 | value of this flag any other time. | |
925 | .sp | |
926 | .ti -0.4i | |
927 | \fB-x\fR If this flag is set, the shell will print out each | |
928 | command before executing it. | |
929 | .sp | |
930 | .ti -0.4i | |
931 | \fB-z\fR If this flag is set, the file name generation process | |
932 | may generate zero files. If it is not set, then a pattern which does | |
933 | not match any files will be replaced by a quoted version of the pattern. | |
934 | .in -0.4i | |
935 | .sp | |
936 | The third use of the set command is to set the values of the shell's | |
937 | positional parameters to the specified | |
938 | .IR args . | |
939 | To change the positional parameters without changing any options, | |
940 | use ``\fB--\fR'' as the first argument to | |
941 | .IR set . | |
942 | If no args are present, the set command will leave the value of the | |
943 | positional parameters unchanged, so to set the positional parameters | |
944 | to set of values that may be empty, execute the command | |
945 | .d | |
946 | shift $# | |
947 | .e | |
948 | first to clear out the old values of the positional parameters. | |
949 | .b setvar | |
950 | .I variable | |
951 | .I value | |
952 | .br | |
953 | Assigns | |
954 | .I value | |
955 | to | |
956 | .IR variable . | |
957 | (In general it is better to write | |
958 | .I variable=value | |
959 | rather than using | |
960 | .IR setvar . | |
961 | .I Setvar | |
962 | is intended to be used in functions that assign values to variables whose | |
963 | names are passed as parameters.) | |
964 | .b shift | |
965 | [ | |
966 | .I n | |
967 | ] | |
968 | .br | |
969 | Shift the positional parameters | |
970 | .I n | |
971 | times. | |
972 | A shift sets the value of $1 to the value of $2, the value of $2 to | |
973 | the value of $3, and so on, decreasing the value of $# by one. | |
974 | If there are zero positional parameters, shifting doesn't do anything. | |
975 | .b trap | |
976 | [ | |
977 | .I action | |
978 | ] | |
979 | .IR signal ... | |
980 | .br | |
981 | Cause the shell to parse and execute | |
982 | .I action | |
983 | when any of the specified signals are received. | |
984 | The signals are specified by signal number. | |
985 | .I Action | |
986 | may be null or omitted; | |
987 | the former causes the specified signal to be ignored and the latter | |
988 | causes the default action to be taken. | |
989 | When the shell forks off a subshell, it resets trapped (but not ignored) | |
990 | signals to the default action. | |
991 | The trap command has no effect on signals that were ignored on entry | |
992 | to the shell. | |
993 | .b umask | |
994 | [ | |
995 | .I mask | |
996 | ] | |
997 | .br | |
998 | Set the value of umask (see | |
999 | .IR umask (2)) | |
1000 | to the specified octal value. If the argument is omitted, the umask | |
1001 | value is printed. | |
1002 | .b unset | |
1003 | .IR name ... | |
1004 | .br | |
1005 | The specified variables and functions are unset and unexported. | |
1006 | If a given name corresponds to both a variable and a function, both the | |
1007 | variable and the function are unset. | |
1008 | .b wait | |
1009 | [ | |
1010 | .I job | |
1011 | ] | |
1012 | .br | |
1013 | Wait for the specified job to complete and return the exit status of the | |
1014 | last process in the job. If the argument is omitted, wait for all jobs | |
1015 | to complete and the return an exit status of zero. | |
1016 | .SH EXAMPLES | |
1017 | The following function redefines the \fIcd\fR command: | |
1018 | .d | |
1019 | cd() { | |
1020 | if bltin cd "$@" | |
1021 | then if test -f .enter | |
1022 | then . .enter | |
1023 | else return 0 | |
1024 | fi | |
1025 | fi | |
1026 | } | |
1027 | .e | |
1028 | This function causes the file ``.enter'' to be read when you enter a | |
1029 | directory, if it exists. The \fIbltin\fR command is used to access the | |
1030 | real \fIcd\fR command. The ``return 0'' ensures that the function will | |
1031 | return an exit status of zero if it successfully changes to a directory | |
1032 | that does not contain a ``.enter'' file. Redefining existing commands | |
1033 | is not always a good idea, but this example shows that you can do it if | |
1034 | you want to. | |
1035 | .PP | |
1036 | The suspend function distributed with | |
1037 | .I ash | |
1038 | looks like | |
1039 | .d | |
1040 | # Copyright (C) 1989 by Kenneth Almquist. All rights reserved. | |
1041 | # This file is part of ash, which is distributed under the terms | |
1042 | # specified by the Ash General Public License. | |
1043 | ||
1044 | suspend() { | |
1045 | local - | |
1046 | set +j | |
1047 | kill -TSTP 0 | |
1048 | } | |
1049 | .e | |
1050 | This turns off job control and then sends a stop signal to the current | |
1051 | process group, which suspends the shell. (When job control is turned | |
1052 | on, the shell ignores the TSTP signal.) Job control will be turned back | |
1053 | on when the function returns because ``-'' is local to the function. | |
1054 | As an example of what \fInot\fR to do, consider an earlier version of | |
1055 | \fIsuspend\fR: | |
1056 | .d | |
1057 | suspend() { | |
1058 | suspend_flag=$- | |
1059 | set +j | |
1060 | kill -TSTP 0 | |
1061 | set -$suspend_flag | |
1062 | } | |
1063 | .e | |
1064 | There are two problems with this. First, \fBsuspend_flag\fR is a global | |
1065 | variable rather than a local one, which will cause problems in the | |
1066 | (unlikely) circumstance that the user is using that variable for some | |
1067 | other purpose. Second, consider what happens if shell received an interrupt | |
1068 | signal after it executes the first \fIset\fR command but before it executes | |
1069 | the second one. The interrupt signal will abort the shell function, so | |
1070 | that the second \fIset\fR command will never be executed and job control | |
1071 | will be left off. The first version of \fIsuspend\fR avoids this problem | |
1072 | by turning job control off only in a local copy of the shell options. The | |
1073 | local copy of the shell options is discarded when the function is terminated, | |
1074 | no matter how it is terminated. | |
1075 | .SH HINTS | |
1076 | Shell variables can be used to provide abbreviations for things which | |
1077 | you type frequently. For example, I set | |
1078 | .br | |
1079 | \h'1i'export h=$HOME | |
1080 | .br | |
1081 | in my .profile so that I can type the name of my home directory simply | |
1082 | by typing ``$h''. | |
1083 | .PP | |
1084 | When writing shell procedures, try not to make assumptions about what is | |
1085 | imported from the environment. Explicitly unset or initialize all variables, | |
1086 | rather than assuming they will be unset. If you use cd, it is a good idea | |
1087 | to unset CDPATH. | |
1088 | .PP | |
1089 | People sometimes use ``<&-'' or ``>&-'' to provide no input to a command | |
1090 | or to discard the output of a command. A better way to do this is | |
1091 | to redirect the input or output of the command to | |
1092 | .BR /dev/null . | |
1093 | .PP | |
1094 | Word splitting and file name generation are performed by default, | |
1095 | and you have to explicitly use double quotes to suppress it. This is | |
1096 | backwards, but you can learn to live with it. Just get in the habit of | |
1097 | writing double quotes around variable and command substitutions, and | |
1098 | omit them only when you really want word splitting and file name generation. | |
1099 | If you want word splitting but not file name generation, use the | |
1100 | .B -f | |
1101 | option. | |
1102 | .SH AUTHORS | |
1103 | Kenneth Almquist | |
1104 | .SH "SEE ALSO" | |
1105 | echo(1), expr(1), line(1), pwd(1), true(1). | |
1106 | .SH BUGS | |
1107 | When command substitution occurs inside a here document, the commands inside | |
1108 | the here document are run with their standard input closed. For example, | |
1109 | the following will not word because the standard input of the | |
1110 | .I line | |
1111 | command will be closed when the command is run: | |
1112 | .d | |
1113 | cat <<-! | |
1114 | Line 1: $(line) | |
1115 | Line 2: $(line) | |
1116 | ! | |
1117 | .e | |
1118 | .PP | |
1119 | Unsetting a function which is currently being executed may cause strange | |
1120 | behavior. | |
1121 | .PP | |
1122 | The shell syntax allows a here document to be terminated by an end of file | |
1123 | as well as by a line containing the terminator word which follows the ``<<''. | |
1124 | What this means is that if you mistype the terminator line, the shell | |
1125 | will silently swallow up the rest of your shell script and stick it | |
1126 | in the here document. |