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