Commit | Line | Data |
---|---|---|
b7aa7294 KB |
1 | .\" Copyright (c) 1991, 1993 |
2 | .\" The Regents of the University of California. All rights reserved. | |
65622797 KB |
3 | .\" |
4 | .\" This code is derived from software contributed to Berkeley by | |
5 | .\" Kenneth Almquist. | |
6 | .\" | |
7 | .\" %sccs.include.redist.man% | |
8 | .\" | |
b7aa7294 | 9 | .\" @(#)sh.1 8.1 (Berkeley) %G% |
65622797 | 10 | .\" |
7ab274c2 | 11 | .na |
dd00107c MT |
12 | .TH SH 1 |
13 | .SH NAME | |
376e2f43 | 14 | sh \- command interpreter (shell) |
dd00107c | 15 | .SH SYNOPSIS |
376e2f43 | 16 | sh [-/+aCefnuvxIimsVEb] [-/+o longname] [arg ...] |
dd00107c MT |
17 | .SH DESCRIPTION |
18 | .LP | |
19 | Sh is the standard command interpreter for the system. The current | |
20 | version | |
21 | of sh is in the process of being changed to | |
22 | conform with the POSIX 1003.2 and 1003.2a specificiations for | |
23 | the shell. This version has many features which make it appear | |
24 | similar in some respects to the Korn shell, but it is not a Korn | |
25 | shell clone (run GNU's bash if you want that). Only features | |
26 | designated by POSIX, plus a few Berkeley extensions, are being | |
27 | incorporated into this shell. We expect POSIX conformance by the | |
28 | time 4.4 BSD is released. | |
29 | This man page is not intended to be a tutorial or a complete | |
30 | specification of the shell. | |
31 | .sp 2 | |
32 | .B Overview | |
65622797 | 33 | .sp |
dd00107c MT |
34 | .LP |
35 | The shell is a command that reads lines from | |
36 | either a file or the terminal, interpretes them, and | |
68e34ceb | 37 | generally executes other commands. It is the program that is running |
dd00107c MT |
38 | when a user logs into the system (although a user can select |
39 | a different shell with the chsh(1) command). | |
40 | The shell | |
7ab274c2 | 41 | implements a language that has flow control contructs, |
dd00107c MT |
42 | a macro facility that provides a variety of features in |
43 | addition to data storage, along with built in history and line | |
44 | editing capabilities. It incorporates many features to | |
45 | aid interactive use and has the advantage that the interpretative | |
46 | language is common to both interactive and non-interactive | |
47 | use (shell scripts). That is, commands can be typed directly | |
7ab274c2 | 48 | to the running shell or can be put into a file and the file |
dd00107c MT |
49 | can be executed directly by the shell. |
50 | .sp 2 | |
51 | .B Invocation | |
65622797 | 52 | .sp |
dd00107c MT |
53 | .LP |
54 | If no args are present and if the standard input of the shell | |
55 | is connected to a terminal (or if the -i flag is set), the shell | |
56 | is considered an interactive shell. An interactive shell | |
57 | generally prompts before each command and handles programming | |
58 | and command errors differently (as described below). | |
59 | When first starting, the shell inspects argument 0, and | |
7ab274c2 | 60 | if it begins with a dash '-', the shell is also considered |
dd00107c MT |
61 | a login shell. This is normally done automatically by the system |
62 | when the user first logs in. A login shell first reads commands | |
63 | from the files /etc/profile and .profile if they exist. | |
64 | If the environment variable ENV is set on entry to a shell, | |
65 | or is set in the .profile of a login shell, the shell next reads | |
66 | commands from the file named in ENV. Therefore, a user should | |
67 | place commands that are to be executed only at login time in | |
68 | the .profile file, and commands that are executed for every | |
69 | shell inside the ENV file. To set the ENV variable to some | |
70 | file, place the following line in your .profile of your home | |
71 | directory | |
72 | .nf | |
73 | ||
74 | ENV=$HOME/.shinit; export ENV | |
75 | ||
76 | .fi | |
7ab274c2 | 77 | substituting for ``.shinit'' any filename you wish. |
dd00107c MT |
78 | Since the ENV file is read for |
79 | every invocation of the shell, including shell scripts and | |
80 | non-interactive shells, the following paradigm is useful | |
81 | for restricting commands in the ENV file to interactive invocations. | |
82 | Place commands within the ``case'' and ``esac'' below (these | |
83 | commands are described later): | |
65622797 | 84 | .nf |
dd00107c MT |
85 | |
86 | case $- in *i*) | |
87 | # commands for interactive use only | |
88 | ... | |
89 | esac | |
90 | ||
65622797 | 91 | .fi |
dd00107c MT |
92 | If command line arguments besides the options have been |
93 | specified, then the shell treats the first argument as the | |
94 | name of a file from which to read commands (a shell script), and | |
95 | the remaining arguments are set as the positional parameters | |
96 | of the shell ($1, $2, etc). Otherwise, the shell reads commands | |
97 | from its standard input. | |
98 | .sp 2 | |
99 | .B Argument List Processing | |
65622797 | 100 | .sp |
dd00107c MT |
101 | .LP |
102 | All of the single letter options have a corresponding name | |
103 | that can be used as an argument to the '-o' option. The | |
104 | set -o name is provided next to the single letter option in | |
105 | the description below. | |
7ab274c2 MT |
106 | Specifying a dash ``-'' turns the option on, while using a plus ``+'' |
107 | disables the option. | |
dd00107c MT |
108 | The following options can be set from the command line or |
109 | with the set(1) builtin (described later). | |
110 | .TP | |
111 | -a allexport | |
7ab274c2 | 112 | Export all variables assigned to. |
dd00107c MT |
113 | (UNIMPLEMENTED for 4.4alpha) |
114 | .TP | |
115 | -C noclobber | |
7ab274c2 | 116 | Don't overwite existing files with ``>''. |
dd00107c MT |
117 | (UNIMPLEMENTED for 4.4alpha) |
118 | .TP | |
119 | -e errexit | |
120 | If not interactive, exit immediatly if any | |
121 | untested command fails. | |
7ab274c2 MT |
122 | The exit status of a command is considered to be |
123 | explicitly tested if the command is used to control | |
124 | an if, elif, while, or until; or if the command is the left | |
125 | hand operand of an ``&&'' or ``||'' operator. | |
126 | ||
dd00107c MT |
127 | .TP |
128 | -f noglob | |
129 | Disable pathname expansion. | |
130 | .TP | |
131 | -n noexec | |
132 | If not interactive, read commands but do not | |
133 | execute them. This is useful for checking the | |
134 | syntax of shell scripts. | |
135 | .TP | |
136 | -u nounset | |
137 | Write a message to standard error when attempting | |
138 | to expand a variable that is not set, and if the | |
139 | shell is not interactive, exit immediatly. | |
140 | (UNIMPLEMENTED for 4.4alpha) | |
141 | .TP | |
142 | -v verbose | |
143 | The shell writes its input to standard error | |
144 | as it is read. Useful for debugging. | |
145 | .TP | |
146 | -x xtrace | |
7ab274c2 MT |
147 | Write each command to standard error (preceded |
148 | by a '+ ') before it is executed. Useful for | |
dd00107c MT |
149 | debugging. |
150 | .TP | |
151 | -I ignoreeof | |
152 | Ignore EOF's from input when interactive. | |
153 | .TP | |
154 | -i interactive | |
155 | Force the shell to behave interactively. | |
156 | .TP | |
157 | -m monitor | |
158 | Turn on job control (set automatically when | |
159 | interactive). | |
160 | .TP | |
161 | -s stdin | |
162 | Read commands from standard input (set automatically | |
163 | if no file arguments are present). This option has | |
164 | no effect when set after the shell has already started | |
165 | running (i.e. with set(1)). | |
166 | .TP | |
167 | -V vi | |
168 | Enable the builtin vi(1) command line editor (disables | |
169 | -E if it has been set). | |
170 | .TP | |
171 | -E emacs | |
172 | Enable the builtin emacs(1) command line editor (disables | |
173 | -V if it has been set). | |
174 | .TP | |
175 | -b notify | |
176 | Enable asychronous notification of background job | |
177 | completion. | |
178 | (UNIMPLEMENTED for 4.4alpha) | |
179 | .LP | |
180 | .sp 2 | |
181 | .B Lexical Structure | |
182 | .sp | |
183 | .LP | |
184 | The shell reads input in terms of lines from a file and breaks | |
7ab274c2 MT |
185 | it up into words at whitespace (blanks and tabs), and at |
186 | certain sequences of | |
dd00107c MT |
187 | characters that are special to the shell called ``operators''. |
188 | There are two types of operators: control operators and | |
189 | redirection operators (their meaning is discussed later). | |
190 | Following is a list of operators: | |
191 | .nf | |
192 | .sp | |
193 | Control operators: & && ( ) ; ;; | || <newline> | |
194 | .sp | |
195 | Redirection operator: < > >| << >> <& >& <<- <> | |
196 | .sp | |
197 | .fi | |
198 | .sp 2 | |
199 | .B Quoting | |
200 | .sp | |
201 | .LP | |
202 | Quoting is used to remove the special meaning of certain characters | |
203 | or words to the shell, such as operators, whitespace, or | |
204 | keywords. There are three types of quoting: matched single quotes, | |
205 | matched double quotes, and backslash. | |
206 | .sp 2 | |
207 | .B Backslash | |
208 | .sp | |
209 | .LP | |
210 | A backslash preserves the literal meaning of the following | |
211 | character, with the exception of <newline>. A backslash preceding | |
212 | a <newline> is treated as a line continuation. | |
213 | .sp 2 | |
214 | .B Single Quotes | |
215 | .sp | |
216 | .LP | |
217 | Enclosing characters in single quotes preserves the literal | |
218 | meaning of all the characters. | |
219 | .sp 2 | |
220 | .B Double Quotes | |
221 | .sp | |
222 | .LP | |
223 | Enclosing characters within double quotes preserves the literal | |
224 | meaning of all characters except dollarsign ($), backquote (`), | |
7ab274c2 | 225 | and backslash (\\). The backslash inside double quotes is |
dd00107c | 226 | historically weird, and serves to quote only the following |
7ab274c2 | 227 | characters: $ ` " \\ <newline>. |
dd00107c MT |
228 | Otherwise it remains literal. |
229 | .sp 2 | |
230 | .B Reserved Words | |
231 | .sp | |
232 | .LP | |
233 | Reserved words are words that have special meaning to the | |
234 | shell and are recognized at the beginning of a line and | |
235 | after a control operator. The following are reserved words: | |
236 | .nf | |
237 | ||
238 | ! elif fi while case | |
239 | else for then { } | |
240 | do done until if esac | |
241 | ||
242 | .fi | |
7ab274c2 | 243 | Their meaning is discussed later. |
dd00107c MT |
244 | .sp 2 |
245 | .B Aliases | |
246 | .sp | |
247 | .LP | |
7ab274c2 | 248 | An alias is a name and corresponding value set using the alias(1) |
dd00107c MT |
249 | builtin command. Whenever a reserved word may occur (see above), |
250 | and after checking for reserved words, the shell | |
251 | checks the word to see if it matches an alias. If it does, | |
252 | it replaces it in the input stream with its value. For example, | |
253 | if there is an alias called ``lf'' with the value ``ls -F'', | |
254 | then the input | |
255 | .nf | |
256 | ||
257 | lf foobar <return> | |
258 | ||
259 | would become | |
260 | ||
261 | ls -F foobar <return> | |
262 | ||
263 | .fi | |
264 | .LP | |
265 | Aliases provide a convenient way for naive users to | |
266 | create shorthands for commands without having to learn how | |
267 | to create functions with arguments. They can also be | |
268 | used to create lexically obscure code. This use is discouraged. | |
269 | .sp 2 | |
270 | .B Commands | |
271 | .sp | |
272 | .LP | |
273 | The shell interpretes the words it reads according to a | |
274 | language, the specification of which is outside the scope | |
275 | of this man page (refer to the BNF in the POSIX 1003.2 | |
276 | document). Essentially though, a line is read and if | |
277 | the first word of the line (or after a control operator) | |
278 | is not a reserved word, then the shell has recognized a | |
279 | simple command. Otherwise, a complex command or some | |
280 | other special construct may have been recognized. | |
281 | .sp 2 | |
282 | .B Simple Commands | |
283 | .sp | |
284 | .LP | |
285 | If a simple command has been recognized, the shell performs | |
286 | the following actions: | |
287 | .sp | |
288 | 1) Leading words of the form ``name=value'' are | |
289 | stripped off and assigned to the environment of | |
290 | the simple command. Redirection operators and | |
291 | their arguments (as described below) are stripped | |
292 | off and saved for processing. | |
293 | .sp | |
294 | 2) The remaining words are expanded as described in | |
295 | the section called ``Expansions'', and the | |
296 | first remaining word is considered the command | |
297 | name and the command is located. The remaining | |
298 | words are considered the arguments of the command. | |
299 | If no command name resulted, then the ``name=value'' | |
300 | variable assignments recognized in 1) affect the | |
301 | current shell. | |
302 | .sp | |
303 | 3) Redirections are performed as described in | |
304 | the next section. | |
305 | .sp 2 | |
306 | .B Redirections | |
307 | .sp | |
308 | .LP | |
309 | Redirections are used to change where a command reads its input | |
310 | or sends its output. In general, redirections open, close, or | |
311 | duplicate an existing reference to a file. The overall format | |
312 | used for redirection is: | |
313 | .nf | |
314 | ||
315 | [n] redir-op file | |
316 | ||
317 | .fi | |
7ab274c2 MT |
318 | where redir-op is one of the redirection operators mentioned |
319 | previously. Following is a list of the possible redirections. | |
dd00107c MT |
320 | The [n] is an optional number, as in '3' (not '[3]'), that |
321 | refers to a file descriptor. | |
322 | .TP | |
323 | [n]> file | |
324 | Redirect standard output (or n) to file. | |
325 | .TP | |
326 | [n]>| file | |
327 | Same, but override the -C option. | |
328 | .TP | |
329 | [n]>> file | |
330 | Append standard output (or n) to file. | |
331 | .TP | |
332 | [n]< file | |
333 | Redirect standard input (or n) from file. | |
334 | .TP | |
335 | [n1]<&n2 | |
336 | Duplicate standard input (or n1) from | |
337 | file descriptor n2. | |
338 | .TP | |
339 | [n]<&- | |
340 | Close standard input (or n). | |
341 | .TP | |
342 | [n1]>&n2 | |
343 | Duplicate standard output (or n) from | |
344 | n2. | |
345 | .TP | |
346 | [n]>&- | |
347 | Close standard output (or n). | |
348 | .TP | |
349 | [n]<> file | |
350 | Open file for reading and writing on | |
351 | standard input (or n). | |
352 | .LP | |
353 | The following redirection is often called a ``here-document''. | |
354 | .nf | |
355 | ||
356 | [n]<< delimiter | |
357 | here-doc-text... | |
358 | delimiter | |
359 | ||
360 | .fi | |
361 | All the text on successive lines up to the delimiter is | |
362 | saved away and made available to the command on standard | |
363 | input, or file descriptor n if it is specified. If the delimiter | |
364 | as specified on the initial line is quoted, then the here-doc-text | |
365 | is treated literally, otherwise the text is subjected to | |
366 | parameter expansion, command substitution, and arithmetic | |
367 | expansion (as described in the section on ``Expansions''). If | |
368 | the operator is ``<<-'' instead of ``<<'', then leading tabs | |
369 | in the here-doc-text are stripped. | |
370 | .sp 2 | |
371 | .B Search and Execution | |
372 | .sp | |
373 | .LP | |
7ab274c2 | 374 | There are three types of commands: shell functions, builtin commands, and normal programs -- and the |
dd00107c MT |
375 | command is searched for (by name) in that order. They |
376 | each are executed in a different way. | |
377 | .LP | |
7ab274c2 MT |
378 | When a shell function is executed, all of the shell positional parameters (except $0, which remains unchanged) are |
379 | set to the arguments of the shell function. The variables which are explicitly placed in the environment of | |
dd00107c MT |
380 | the command (by placing assignments to them before the |
381 | function name) are made local to the function and are set | |
382 | to the values given. Then the command given in the function | |
383 | definition is executed. The positional parameters are | |
7ab274c2 | 384 | restored to their original values when the command completes. |
dd00107c | 385 | .LP |
7ab274c2 | 386 | Shell builtins are executed internally to the shell, without spawning a new process. |
dd00107c MT |
387 | .LP |
388 | Otherwise, if the command name doesn't match a function | |
389 | or builtin, the command is searched for as a normal | |
390 | program in the filesystem (as described in the next section). | |
7ab274c2 | 391 | When a normal program is executed, the shell runs the program, passing the arguments and the environment to the |
dd00107c MT |
392 | program. If the program is a shell procedure, the shell |
393 | will interpret the program in a subshell. The shell will | |
394 | reinitialize itself in this case, so that the effect will | |
395 | be as if a new shell had been invoked to handle the shell | |
396 | procedure, except that the location of commands located in | |
397 | the parent shell will be remembered by the child. | |
398 | .sp 2 | |
399 | .B Path Search | |
400 | .sp | |
401 | .LP | |
402 | When locating a command, the shell first looks to see if | |
403 | it has a shell function by that name. Then it looks for a | |
404 | builtin command by that name. | |
405 | Finally, it searches each | |
406 | entry in PATH in turn for the command. | |
407 | .LP | |
408 | The value of the PATH variable should be a series of | |
409 | entries separated by colons. Each entry consists of a | |
7ab274c2 MT |
410 | directory name. |
411 | The current directory | |
412 | may be indicated by an empty directory name. | |
413 | .LP | |
414 | Command names containing a slash are simply executed without performing any of the above searches. | |
dd00107c MT |
415 | .sp 2 |
416 | .B Command Exit Status | |
417 | .sp | |
418 | .LP | |
419 | Each command has an exit status that can influence the behavior | |
420 | of other shell commands. The paradigm is that a command exits | |
421 | with zero for normal or success, and non-zero for failure, | |
422 | error, or a false indication. The man page for each command | |
423 | should indicate the varius exit codes and what they mean. | |
424 | Additionally, the builtin commands return exit codes, as does | |
425 | an executed function. | |
426 | .sp 2 | |
427 | .B Complex Commands | |
428 | .sp | |
429 | .LP | |
7ab274c2 MT |
430 | Complex commands are combinations of simple commands |
431 | with control operators or reserved words, together creating a larger complex | |
dd00107c MT |
432 | command. More generally, a command is one of the following: |
433 | .nf | |
434 | ||
435 | - simple command | |
436 | ||
437 | - pipeline | |
438 | ||
439 | - list or compound-list | |
440 | ||
441 | - compound command | |
442 | ||
443 | - function definition | |
444 | ||
445 | .fi | |
446 | .LP | |
447 | Unless otherwise stated, the exit status of a command is | |
448 | that of the last simple command executed by the command. | |
449 | .sp 2 | |
450 | .B Pipeline | |
451 | .sp | |
452 | .LP | |
453 | A pipeline is a sequence of one or more commands separated | |
454 | by the control operator |. The standard output of all but | |
455 | the last command is connected to the standard input | |
456 | of the next command. | |
457 | .LP | |
458 | The format for a pipeline is: | |
459 | .nf | |
460 | ||
461 | [!] command1 [ | command2 ...] | |
462 | ||
463 | .fi | |
464 | .LP | |
465 | The standard output of command1 is connected to the standard | |
466 | input of command2. The standard input, standard output, or | |
467 | both of a command is considered to be assigned by the | |
468 | pipeline before any redirection specified by redirection | |
469 | operators that are part of the command. | |
470 | .LP | |
471 | If the pipeline is not in the background (discussed later), | |
472 | the shell waits for all commands to complete. | |
473 | .LP | |
474 | If the reserved word ! does not precede the pipeline, the | |
475 | exit status is the exit status of the last command specified | |
476 | in the pipeline. Otherwise, the exit status is the logical | |
477 | NOT of the exit status of the last command. That is, if | |
478 | the last command returns zero, the exit status is 1; if | |
479 | the last command returns greater than zero, the exit status | |
65622797 | 480 | is zero. |
dd00107c MT |
481 | .LP |
482 | Because pipeline assignment of standard input or standard | |
483 | output or both takes place before redirection, it can be | |
484 | modified by redirection. For example: | |
485 | .nf | |
486 | ||
487 | $ command1 2>&1 | command2 | |
488 | ||
dcd7db01 | 489 | .fi |
dd00107c MT |
490 | sends both the standard output and standard error of command1 |
491 | to the standard input of command2. | |
492 | .LP | |
493 | A ; or <newline> terminator causes the preceding | |
7ab274c2 | 494 | AND-OR-list (described next) to be executed sequentially; a & causes |
dd00107c MT |
495 | asynchronous execution of the preceding AND-OR-list. |
496 | .sp 2 | |
497 | .B Background Commands -- & | |
498 | .sp | |
499 | .LP | |
500 | If a command is terminated by the control operator ampersand | |
501 | (&), the shell executes the command asynchronously -- that is, | |
502 | the shell does not wait for | |
503 | the command to finish before executing the next command. | |
504 | .LP | |
505 | The format for running a command in background is: | |
506 | .nf | |
507 | ||
508 | command1 & [command2 & ...] | |
509 | ||
510 | .fi | |
511 | If the shell is not interactive, the standard input of an | |
512 | asychronous command is set to /dev/null. | |
513 | .sp 2 | |
514 | .B Lists -- Generally Speaking | |
515 | .sp | |
516 | .LP | |
517 | A list is a sequence of zero or more commands separated by | |
7ab274c2 | 518 | newlines, semicolons, or ampersands, and optionally terminated by one of these three characters. |
dd00107c | 519 | The commands in a |
7ab274c2 | 520 | list are executed in the order they are written. If command is followed by an ampersand, the shell starts the |
dd00107c MT |
521 | command and immediately proceed onto the next command; |
522 | otherwise it waits for the command to terminate before | |
523 | proceeding to the next one. | |
524 | .LP | |
7ab274c2 | 525 | ``&&'' and ``||'' are AND-OR list operators. ``&&'' executes |
dd00107c MT |
526 | the first command, and then executes the second command |
527 | iff the exit status of the first command is zero. ``||'' | |
528 | is similar, but executes the second command iff the exit | |
529 | status of the first command is nonzero. ``&&'' and ``||'' | |
530 | both have the same priority. | |
531 | .LP | |
dcd7db01 | 532 | The syntax of the if command is |
dd00107c MT |
533 | .nf |
534 | ||
535 | if list | |
536 | then list | |
537 | [ elif list | |
538 | then list ] ... | |
539 | [ else list ] | |
540 | fi | |
541 | ||
542 | .fi | |
dcd7db01 | 543 | The syntax of the while command is |
dd00107c MT |
544 | .nf |
545 | ||
546 | while list | |
547 | do list | |
548 | done | |
549 | ||
550 | .fi | |
7ab274c2 | 551 | The two lists are executed repeatedly while the exit status of the first list is zero. The until command is similar, but has the word until in place of while |
dd00107c MT |
552 | repeats until the exit status of the first list is zero. |
553 | .LP | |
dcd7db01 | 554 | The syntax of the for command is |
dd00107c | 555 | .nf |
dcd7db01 | 556 | |
dd00107c MT |
557 | for variable in word... |
558 | do list | |
559 | done | |
560 | ||
561 | .fi | |
562 | The words are expanded, and then the list is executed | |
563 | repeatedly with the variable set to each word in turn. do | |
564 | and done may be replaced with ``{'' and ``}''. | |
565 | .LP | |
dcd7db01 | 566 | The syntax of the break and continue command is |
dd00107c MT |
567 | .nf |
568 | ||
569 | break [ num ] | |
570 | continue [ num ] | |
571 | ||
572 | .fi | |
573 | Break terminates the num innermost for or while loops. | |
7ab274c2 | 574 | Continue continues with the next iteration of the innermost loop. These are implemented as builtin commands. |
dd00107c | 575 | .LP |
dcd7db01 | 576 | The syntax of the case command is |
dd00107c MT |
577 | .nf |
578 | ||
579 | case word in | |
580 | pattern) list ;; | |
581 | ... | |
582 | esac | |
583 | ||
584 | .fi | |
585 | .LP | |
586 | The pattern can actually be one or more patterns (see Shell | |
587 | Patterns described later), separated by ``|'' characters. | |
588 | ||
589 | .LP | |
65622797 | 590 | Commands may be grouped by writing either |
dd00107c MT |
591 | .nf |
592 | ||
593 | (list) | |
594 | ||
595 | .fi | |
65622797 | 596 | or |
dd00107c MT |
597 | .nf |
598 | ||
599 | { list; } | |
600 | ||
601 | .fi | |
65622797 | 602 | The first of these executes the commands in a subshell. |
7ab274c2 MT |
603 | .sp 2 |
604 | .B Functions | |
605 | .sp | |
dd00107c | 606 | .LP |
dcd7db01 | 607 | The syntax of a function definition is |
65622797 | 608 | .nf |
dd00107c MT |
609 | |
610 | name ( ) command | |
611 | ||
612 | .fi | |
613 | .LP | |
614 | A function definition is an executable statement; when | |
615 | executed it installs a function named name and returns an | |
616 | exit status of zero. The command is normally a list | |
617 | enclosed between ``{'' and ``}''. | |
618 | .LP | |
619 | Variables may be declared to be local to a function by | |
620 | using a local command. This should appear as the first | |
dcd7db01 | 621 | staement of a function, and the syntax is |
dd00107c MT |
622 | .nf |
623 | ||
624 | local [ variable | - ] ... | |
625 | ||
626 | .fi | |
627 | Local is implemented as a builtin command. | |
628 | .LP | |
629 | When a variable is made local, it inherits the initial | |
630 | value and exported and readonly flags from the variable | |
631 | with the same name in the surrounding scope, if there is | |
7ab274c2 | 632 | one. Otherwise, the variable is initially unset. The shell |
dd00107c | 633 | uses dynamic scoping, so that if you make the variable x |
7ab274c2 | 634 | local to function f, which then calls function g, references to the variable x made inside g will refer to the |
dd00107c MT |
635 | variable x declared inside f, not to the global variable |
636 | named x. | |
637 | .LP | |
638 | The only special parameter than can be made local is | |
639 | ``-''. Making ``-'' local any shell options that are | |
640 | changed via the set command inside the function to be | |
641 | restored to their original values when the function | |
642 | returns. | |
643 | .LP | |
dcd7db01 | 644 | The syntax of the return command is |
dd00107c MT |
645 | .nf |
646 | ||
647 | return [ exitstatus ] | |
648 | ||
649 | .fi | |
650 | It terminates the currently executing function. Return is | |
651 | implemented as a builtin command. | |
652 | .sp 2 | |
653 | .B Variables and Parameters | |
654 | .sp | |
655 | .LP | |
656 | The shell maintains a set of parameters. A parameter | |
657 | denoted by a name is called a variable. When starting up, | |
658 | the shell turns all the environment variables into shell | |
659 | variables. New variables can be set using the form | |
660 | .nf | |
661 | ||
662 | name=value | |
663 | ||
65622797 | 664 | .fi |
dd00107c MT |
665 | .LP |
666 | Variables set by the user must have a name consisting solely | |
667 | of alphabetics, numerics, and underscores - the first of which | |
668 | must not be numeric. A parameter can also be denoted by a number | |
669 | or a special character as explained below. | |
670 | .sp 2 | |
671 | .B Positional Parameters | |
65622797 | 672 | .sp |
dd00107c MT |
673 | .LP |
674 | A positional parameter is a parameter denoted by a number (n > 0). | |
675 | The shell sets these initially to the values of its command | |
676 | line arguements that follow the name of the shell script. | |
677 | The set(1) builtin can also be used to set or reset them. | |
678 | .sp 2 | |
7ab274c2 | 679 | .B Special Parameters |
65622797 | 680 | .sp |
dd00107c MT |
681 | .LP |
682 | A special parameter is a parameter denoted by one of the following | |
683 | special characters. The value of the parameter is listed | |
684 | next to its character. | |
685 | .TP | |
686 | * | |
687 | Expands to the positional parameters, starting from one. When | |
688 | the expansion occurs within a double-quoted string | |
689 | it expands to a single field with the value of each parameter | |
690 | separated by the first character of the IFS variable, or by a | |
691 | <space> if IFS is unset. | |
692 | .TP | |
693 | @ | |
694 | Expands to the positional parameters, starting from one. When | |
695 | the expansion occurs within double-quotes, each positional | |
696 | parameter expands as a separate argument. | |
697 | If there are no positional parameters, the | |
7ab274c2 | 698 | expansion of @ generates zero arguments, even when @ is |
dd00107c MT |
699 | double-quoted. What this basically means, for example, is |
700 | if $1 is ``abc'' and $2 is ``def ghi'', then "$@" expands to | |
701 | the two arguments: | |
702 | ||
703 | "abc" "def ghi" | |
704 | .TP | |
705 | # | |
706 | Expands to the number of positional parameters. | |
707 | .TP | |
708 | ? | |
709 | Expands to the exit status of the most recent pipeline. | |
710 | .TP | |
711 | - (Hyphen) | |
712 | Expands to the current option flags (the single-letter | |
713 | option names concatenated into a string) as specified on | |
714 | invocation, by the set builtin command, or implicitly | |
715 | by the shell. | |
716 | .TP | |
717 | $ | |
718 | Expands to the process ID of the invoked shell. A subshell | |
719 | retains the same value of $ as its parent. | |
720 | .TP | |
721 | ! | |
722 | Expands to the process ID of the most recent background | |
723 | command executed from the current shell. For a | |
724 | pipeline, the process ID is that of the last command in the | |
725 | pipeline. | |
726 | .TP | |
727 | 0 (Zero.) | |
728 | Expands to the name of the shell or shell script. | |
729 | .LP | |
730 | .sp 2 | |
731 | .B Word Expansions | |
65622797 | 732 | .sp |
dd00107c MT |
733 | .LP |
734 | This clause describes the various expansions that are | |
735 | performed on words. Not all expansions are performed on | |
736 | every word, as explained later. | |
737 | .LP | |
738 | Tilde expansions, parameter expansions, command substitutions, | |
739 | arithmetic expansions, and quote removals that occur within | |
740 | a single word expand to a single field. It is only field | |
741 | splitting or pathname expansion that can create multiple | |
742 | fields from a single word. The single exception to this | |
743 | rule is the expansion of the special parameter @ within | |
7ab274c2 | 744 | double-quotes, as was described above. |
dd00107c MT |
745 | .LP |
746 | The order of word expansion is: | |
747 | .LP | |
748 | (1) Tilde Expansion, Parameter Expansion, Command Substitution, | |
749 | Arithmetic Expansion (these all occur at the same time). | |
750 | .LP | |
751 | (2) Field Splitting is performed on fields | |
752 | generated by step (1) unless the IFS variable is null. | |
753 | .LP | |
754 | (3) Pathname Expansion (unless set -f is in effect). | |
755 | .LP | |
756 | (4) Quote Removal. | |
757 | .LP | |
758 | The $ character is used to introduce parameter expansion, command | |
759 | substitution, or arithmetic evaluation. | |
760 | .sp 2 | |
761 | .B Tilde Expansion (substituting a users home directory) | |
762 | .sp | |
763 | .LP | |
764 | A word beginning with an unquoted tilde character (~) is | |
765 | subjected to tilde expansion. All the characters up to | |
766 | a slash (/) or the end of the word are treated as a username | |
767 | and are replaced with the users home directory. If the | |
7ab274c2 | 768 | username is missing (as in ~/foobar), the tilde is replaced |
dd00107c MT |
769 | with the value of the HOME variable (the current users |
770 | home directory). | |
771 | ||
772 | .sp 2 | |
773 | .B Parameter Expansion | |
65622797 | 774 | .sp |
dd00107c MT |
775 | .LP |
776 | The format for parameter expansion is as follows: | |
777 | .nf | |
778 | ||
779 | ${expression} | |
780 | ||
781 | .fi | |
782 | where expression consists of all characters until the matching }. Any } | |
783 | escaped by a backslash or within a quoted string, and characters in | |
784 | embedded arithmetic expansions, command substitutions, and variable | |
785 | expansions, are not examined in determining the matching }. | |
786 | .LP | |
787 | The simplest form for parameter expansion is: | |
788 | .nf | |
789 | ||
790 | ${parameter} | |
791 | ||
792 | .fi | |
793 | The value, if any, of parameter is substituted. | |
794 | .LP | |
795 | The parameter name or symbol can be enclosed in braces, which are | |
796 | optional except for positional parameters with more than one digit or | |
797 | when parameter is followed by a character that could be interpreted as | |
7ab274c2 MT |
798 | part of the name. |
799 | If a parameter expansion occurs inside | |
dd00107c MT |
800 | double-quotes: |
801 | .LP | |
7ab274c2 | 802 | 1) Pathname expansion is not performed on the results of the |
dd00107c MT |
803 | expansion. |
804 | .LP | |
7ab274c2 | 805 | 2) Field splitting is not performed on the results of the |
dd00107c MT |
806 | expansion, with the exception of @. |
807 | .LP | |
808 | In addition, a parameter expansion can be modified by using one of the | |
809 | following formats. | |
65622797 | 810 | .sp |
dd00107c MT |
811 | .TP |
812 | ${parameter:-word} | |
813 | Use Default Values. If parameter is unset or | |
814 | null, the expansion of word is | |
815 | substituted; otherwise, the value of | |
816 | parameter is substituted. | |
817 | .TP | |
818 | ${parameter:=word} | |
819 | Assign Default Values. If parameter is unset | |
820 | or null, the expansion of word is | |
821 | assigned to parameter. In all cases, the | |
822 | final value of parameter is | |
823 | substituted. Only variables, not positional | |
824 | parameters or special parameters, can be | |
825 | assigned in this way. | |
826 | .TP | |
827 | ${parameter:?[word]} | |
828 | Indicate Error if Null or Unset. If | |
829 | parameter is unset or null, the expansion of | |
830 | word (or a message indicating it is unset if | |
831 | word is omitted) is written to standard | |
832 | error and the shell exits with a nonzero | |
833 | exit status. Otherwise, the value of | |
834 | parameter is substituted. An | |
835 | interactive shell need not exit. | |
836 | .TP | |
837 | ${parameter:+word} | |
838 | Use Alternate Value. If parameter is unset | |
839 | or null, null is substituted; | |
840 | otherwise, the expansion of word is | |
841 | substituted. | |
842 | .LP | |
843 | In the parameter expansions shown previously, use of the colon in the | |
844 | format results in a test for a parameter that is unset or null; omission | |
845 | of the colon results in a test for a parameter that is only unset. | |
846 | .TP | |
847 | ${#parameter} | |
848 | String Length. The length in characters of | |
7ab274c2 | 849 | the value of parameter. |
dd00107c MT |
850 | .LP |
851 | The following four varieties of parameter expansion provide for substring | |
852 | processing. In each case, pattern matching notation (see Shell Patterns), rather | |
853 | than regular expression notation, is used to evaluate the patterns. | |
854 | If parameter is * or @, the result of the expansion is unspecified. | |
855 | Enclosing the full parameter expansion string in double-quotes does not | |
856 | cause the following four varieties of pattern characters to be quoted, | |
857 | whereas quoting characters within the braces has this effect. | |
858 | (UNIMPLEMENTED IN 4.4alpha) | |
859 | .TP | |
860 | ${parameter%word} | |
861 | Remove Smallest Suffix Pattern. The word | |
862 | is expanded to produce a pattern. The | |
863 | parameter expansion then results in | |
864 | parameter, with the smallest portion of the | |
865 | suffix matched by the pattern deleted. | |
866 | ||
867 | .TP | |
868 | ${parameter%%word} | |
869 | Remove Largest Suffix Pattern. The word | |
870 | is expanded to produce a pattern. The | |
871 | parameter expansion then results in | |
872 | parameter, with the largest portion of the | |
873 | suffix matched by the pattern deleted. | |
874 | .TP | |
875 | ${parameter#word} | |
876 | Remove Smallest Prefix Pattern. The word | |
877 | is expanded to produce a pattern. The | |
878 | parameter expansion then results in | |
879 | parameter, with the smallest portion of the | |
880 | prefix matched by the pattern deleted. | |
881 | .TP | |
882 | ${parameter##word} | |
883 | Remove Largest Prefix Pattern. The word | |
884 | is expanded to produce a pattern. The | |
885 | parameter expansion then results in | |
886 | parameter, with the largest portion of the | |
887 | prefix matched by the pattern deleted. | |
888 | .LP | |
889 | .sp 2 | |
890 | .B Command Substitution | |
65622797 | 891 | .sp |
dd00107c MT |
892 | .LP |
893 | Command substitution allows the output of a command to be substituted in | |
894 | place of the command name itself. Command substitution occurs when | |
895 | the command is enclosed as follows: | |
896 | .nf | |
897 | ||
898 | $(command) | |
899 | ||
900 | .fi | |
901 | or (``backquoted'' version): | |
902 | .nf | |
903 | ||
904 | `command` | |
905 | ||
906 | .fi | |
907 | .LP | |
908 | The shell expands the command substitution by executing command in a | |
909 | subshell environment and replacing the command substitution | |
7ab274c2 | 910 | with the |
dd00107c MT |
911 | standard output of the command, removing sequences of one or more |
912 | <newline>s at the end of the substitution. (Embedded <newline>s before | |
913 | the end of the output are not removed; however, during field | |
914 | splitting, they may be translated into <space>s, depending on the value | |
915 | of IFS and quoting that is in effect.) | |
916 | ||
917 | .sp 2 | |
918 | .B Arithmetic Expansion | |
65622797 | 919 | .sp |
dd00107c MT |
920 | .LP |
921 | Arithmetic expansion provides a mechanism for evaluating an arithmetic | |
922 | expression and substituting its value. The format for arithmetic | |
923 | expansion is as follows: | |
924 | .nf | |
925 | ||
926 | $((expression)) | |
927 | ||
928 | .fi | |
929 | The expression is treated as if it were in double-quotes, except | |
930 | that a double-quote inside the expression is not treated specially. The | |
931 | shell expands all tokens in the expression for parameter expansion, | |
932 | command substitution, and quote removal. | |
933 | .LP | |
934 | Next, the shell treats this as an arithmetic expression and | |
7ab274c2 | 935 | substitutes the value of the expression. |
dd00107c MT |
936 | |
937 | .sp 2 | |
938 | .B White Space Splitting (Field Splitting) | |
65622797 | 939 | .sp |
dd00107c MT |
940 | .LP |
941 | After parameter expansion, command substitution, and | |
942 | arithmetic expansion the shell scans the results of | |
943 | expansions and substitutions that did not occur in double-quotes for | |
944 | field splitting and multiple fields can result. | |
945 | .LP | |
946 | The shell treats each character of the IFS as a delimiter and use | |
947 | the delimiters to split the results of parameter expansion and command | |
948 | substitution into fields. | |
949 | ||
950 | .sp 2 | |
951 | .B Pathname Expansion (File Name Generation) | |
65622797 | 952 | .sp |
dd00107c | 953 | .LP |
7ab274c2 | 954 | Unless the -f flag is set, file name generation is performed after word splitting is complete. Each word is |
dd00107c MT |
955 | viewed as a series of patterns, separated by slashes. The |
956 | process of expansion replaces the word with the names of | |
957 | all existing files whose names can be formed by replacing | |
7ab274c2 MT |
958 | each pattern with a string that matches the specified pattern. |
959 | There are two restrictions on this: first, a pattern cannot match a string containing a slash, and second, | |
dd00107c MT |
960 | a pattern cannot match a string starting with a period |
961 | unless the first character of the pattern is a period. | |
962 | The next section describes the patterns used for both | |
963 | Pathname Expansion and the case(1) command. | |
964 | ||
965 | .sp 2 | |
966 | .B Shell Patterns | |
65622797 | 967 | .sp |
dd00107c | 968 | .LP |
7ab274c2 | 969 | A pattern consists of normal characters, which match themselves, and meta-characters. The meta-characters are |
dd00107c MT |
970 | ``!'', ``*'', ``?'', and ``[''. These characters lose |
971 | there special meanings if they are quoted. When command | |
972 | or variable substitution is performed and the dollar sign | |
973 | or back quotes are not double quoted, the value of the | |
974 | variable or the output of the command is scanned for these | |
975 | characters and they are turned into meta-characters. | |
976 | .LP | |
977 | An asterisk (``*'') matches any string of characters. A | |
978 | question mark matches any single character. A left | |
979 | bracket (``['') introduces a character class. The end of | |
980 | the character class is indicated by a ``]''; if the ``]'' | |
981 | is missing then the ``['' matches a ``['' rather than | |
982 | introducing a character class. A character class matches | |
983 | any of the characters between the square brackets. A | |
984 | range of characters may be specified using a minus sign. | |
985 | The character class may be complemented by making an | |
986 | exclamation point the first character of the character | |
987 | class. | |
988 | .LP | |
989 | To include a ``]'' in a character class, make it the first | |
990 | character listed (after the ``!'', if any). To include a | |
991 | minus sign, make it the first or last character listed | |
992 | ||
993 | .sp 2 | |
994 | .B Builtins | |
65622797 | 995 | .sp |
dd00107c MT |
996 | .LP |
997 | This section lists the builtin commands which | |
998 | are builtin because they need to perform some operation | |
7ab274c2 | 999 | that can't be performed by a separate process. In addition to these, there are several other commands that may |
dd00107c MT |
1000 | be builtin for efficiency (e.g. printf(1), echo(1), test(1), |
1001 | etc). | |
1002 | .TP | |
1003 | alias [ name[=string] ... ] | |
1004 | If name=string is specified, the shell defines the | |
1005 | alias ``name'' with value ``string''. If just ``name'' | |
1006 | is specified, the value of the alias ``name'' is printed. | |
1007 | With no arguments, the alias builtin prints the | |
1008 | names and values of all defined aliases (see unalias). | |
1009 | .TP | |
1010 | bg [ job ] ... | |
1011 | Continue the specified jobs (or the current job if no | |
7ab274c2 | 1012 | jobs are given) in the background. |
dd00107c MT |
1013 | .TP |
1014 | command command arg... | |
7ab274c2 | 1015 | Execute the specified builtin command. (This is useful when you have a shell function with the same name |
dd00107c MT |
1016 | as a builtin command.) |
1017 | .TP | |
1018 | cd [ directory ] | |
1019 | Switch to the specified directory (default $HOME). | |
1020 | If the an entry for CDPATH appears in the environment | |
1021 | of the cd command or the shell variable CDPATH is set | |
1022 | and the directory name does not begin with a slash, | |
1023 | then the directories listed in CDPATH will be | |
1024 | searched for the specified directory. The format of | |
7ab274c2 | 1025 | CDPATH is the same as that of PATH. In an interactive shell, the cd command will print out the name of |
dd00107c MT |
1026 | the directory that it actually switched to if this is |
1027 | different from the name that the user gave. These | |
1028 | may be different either because the CDPATH mechanism | |
1029 | was used or because a symbolic link was crossed. | |
1030 | .TP | |
1031 | \&. file | |
7ab274c2 | 1032 | The commands in the specified file are read and executed by the shell. |
dd00107c MT |
1033 | .TP |
1034 | eval string... | |
1035 | Concatenate all the arguments with spaces. Then | |
1036 | re-parse and execute the command. | |
1037 | .TP | |
1038 | exec [ command arg... ] | |
1039 | Unless command is omitted, the shell process is | |
1040 | replaced with the specified program (which must be a | |
1041 | real program, not a shell builtin or function). Any | |
7ab274c2 | 1042 | redirections on the exec command are marked as permanent, so that they are not undone when the exec command finishes. |
dd00107c MT |
1043 | .TP |
1044 | exit [ exitstatus ] | |
1045 | Terminate the shell process. If exitstatus is given | |
1046 | it is used as the exit status of the shell; otherwise | |
1047 | the exit status of the preceding command is used. | |
1048 | .TP | |
1049 | export name... | |
1050 | The specified names are exported so that they will | |
1051 | appear in the environment of subsequent commands. | |
1052 | The only way to un-export a variable is to unset it. | |
7ab274c2 | 1053 | The shell allows the value of a variable to be set at the |
dd00107c MT |
1054 | same time it is exported by writing |
1055 | .nf | |
1056 | ||
1057 | export name=value | |
1058 | ||
1059 | .fi | |
1060 | With no arguments the export command lists the names | |
1061 | of all exported variables. | |
1062 | .TP | |
1063 | fc [-e editor] [first [last]] | |
1064 | .TP | |
1065 | fc -l [-nr] [first [last]] | |
1066 | .TP | |
1067 | fc -s [old=new] [first] | |
1068 | The fc builtin lists, or edits and re-executes, commands | |
1069 | previously entered to an interactive shell. | |
1070 | .RS +.5i | |
1071 | .TP 2 | |
1072 | -e editor | |
1073 | Use the editor named by editor to edit the commands. The | |
1074 | editor string is a command name, subject to search via the | |
1075 | PATH variable. The value in the FCEDIT variable | |
1076 | is used as a default when -e is not specified. If | |
1077 | FCEDIT is null or unset, the value of the EDITOR | |
1078 | variable is used. If EDITOR is null or unset, | |
1079 | ed(1) is used as the editor. | |
1080 | .TP 2 | |
1081 | -l (ell) | |
1082 | List the commands rather than invoking | |
1083 | an editor on them. The commands are written in the | |
1084 | sequence indicated by the first and last operands, as | |
1085 | affected by -r, with each command preceded by the command | |
1086 | number. | |
1087 | .TP 2 | |
1088 | -n | |
1089 | Suppress command numbers when listing with -l. | |
1090 | .TP 2 | |
1091 | -r | |
1092 | Reverse the order of the commands listed (with -l) or | |
1093 | edited (with neither -l nor -s). | |
1094 | .TP 2 | |
1095 | -s | |
1096 | Re-execute the command without invoking an editor. | |
1097 | .TP 2 | |
1098 | first | |
1099 | .TP 2 | |
1100 | last | |
1101 | Select the commands to list or edit. The number of | |
1102 | previous commands that can be accessed are determined | |
1103 | by the value of the HISTSIZE variable. The value of first | |
1104 | or last or both are one of the following: | |
1105 | .TP 2 | |
1106 | [+]number | |
1107 | A positive number representing a command | |
1108 | number; command numbers can be displayed | |
1109 | with the -l option. | |
1110 | .TP 2 | |
1111 | -number | |
1112 | A negative decimal number representing the | |
1113 | command that was executed number of | |
1114 | commands previously. For example, -1 is | |
1115 | the immediately previous command. | |
1116 | .TP 2 | |
1117 | string | |
1118 | A string indicating the most recently | |
1119 | entered command that begins with that | |
1120 | string. If the old=new operand is not also | |
1121 | specified with -s, the string form of the | |
1122 | first operand cannot contain an embedded | |
1123 | equal sign. | |
1124 | .TP | |
1125 | The following environment variables affect the execution of fc: | |
1126 | .TP 2 | |
1127 | FCEDIT | |
1128 | Name of the editor to use. | |
1129 | .TP 2 | |
1130 | HISTSIZE | |
1131 | The number of previous ocmmands that are accessable. | |
1132 | .RE | |
1133 | .TP | |
1134 | fg [ job ] | |
1135 | Move the specified job or the current job to the | |
7ab274c2 | 1136 | foreground. |
dd00107c MT |
1137 | .TP |
1138 | getopts optstring var | |
1139 | The POSIX getopts command. | |
1140 | .TP | |
1141 | hash -rv command... | |
1142 | The shell maintains a hash table which remembers the | |
1143 | locations of commands. With no arguments whatsoever, | |
1144 | the hash command prints out the contents of this | |
1145 | table. Entries which have not been looked at since | |
1146 | the last cd command are marked with an asterisk; it | |
1147 | is possible for these entries to be invalid. | |
65622797 | 1148 | .sp |
7ab274c2 | 1149 | With arguments, the hash command removes the specified commands from the hash table (unless they are |
dd00107c MT |
1150 | functions) and then locates them. With the -v |
1151 | option, hash prints the locations of the commands as | |
1152 | it finds them. The -r option causes the hash command | |
1153 | to delete all the entries in the hash table except | |
1154 | for functions. | |
1155 | .TP | |
1156 | jobid [ job ] | |
1157 | Print the process id's of the processes in the job. | |
1158 | If the job argument is omitted, use the current job. | |
1159 | .TP | |
1160 | jobs | |
1161 | This command lists out all the background processes | |
1162 | which are children of the current shell process. | |
1163 | .TP | |
1164 | pwd | |
1165 | Print the current directory. The builtin command may | |
1166 | differ from the program of the same name because the | |
1167 | builtin command remembers what the current directory | |
1168 | is rather than recomputing it each time. This makes | |
1169 | it faster. However, if the current directory is | |
1170 | renamed, the builtin version of pwd will continue to | |
1171 | print the old name for the directory. | |
1172 | .TP | |
1173 | read [ -p prompt ] [ -e ] variable... | |
1174 | The prompt is printed if the -p option is specified | |
1175 | and the standard input is a terminal. Then a line is | |
1176 | read from the standard input. The trailing newline | |
1177 | is deleted from the line and the line is split as | |
1178 | described in the section on word splitting above, and | |
1179 | the pieces are assigned to the variables in order. | |
7ab274c2 | 1180 | If there are more pieces than variables, the remaining pieces (along with the characters in IFS that |
dd00107c | 1181 | separated them) are assigned to the last variable. |
7ab274c2 | 1182 | If there are more variables than pieces, the remaining variables are assigned the null string. |
65622797 | 1183 | .sp |
dd00107c MT |
1184 | The -e option causes any backslashes in the input to |
1185 | be treated specially. If a backslash is followed by | |
1186 | a newline, the backslash and the newline will be | |
1187 | deleted. If a backslash is followed by any other | |
7ab274c2 | 1188 | character, the backslash will be deleted and the following character will be treated as though it were |
dd00107c MT |
1189 | not in IFS, even if it is. |
1190 | .TP | |
1191 | readonly name... | |
1192 | The specified names are marked as read only, so that | |
7ab274c2 | 1193 | they cannot be subsequently modified or unset. The shell |
dd00107c MT |
1194 | allows the value of a variable to be set at the same |
1195 | time it is marked read only by writing | |
1196 | .TP | |
1197 | readonly name=value | |
1198 | With no arguments the readonly command lists the | |
1199 | names of all read only variables. | |
1200 | .TP | |
1201 | set [ { -options | +options | -- } ] arg... | |
1202 | The set command performs three different functions. | |
65622797 | 1203 | .sp |
dd00107c MT |
1204 | With no arguments, it lists the values of all shell |
1205 | variables. | |
65622797 | 1206 | .sp |
dd00107c MT |
1207 | If options are given, it sets the specified option |
1208 | flags, or clears them as described in the section | |
1209 | called ``Argument List Processing''. | |
1210 | .sp | |
1211 | The third use of the set command is to set the values | |
1212 | of the shell's positional parameters to the specified | |
1213 | args. To change the positional parameters without | |
7ab274c2 | 1214 | changing any options, use ``--'' as the first argument to set. If no args are present, the set command |
dd00107c MT |
1215 | will clear all the positional parameters (equivalent |
1216 | to executing ``shift $#''. | |
1217 | .TP | |
1218 | setvar variable value | |
1219 | Assigns value to variable. (In general it is better | |
1220 | to write variable=value rather than using setvar. | |
1221 | Setvar is intended to be used in functions that | |
1222 | assign values to variables whose names are passed as | |
1223 | parameters.) | |
1224 | .TP | |
1225 | shift [ n ] | |
1226 | Shift the positional parameters n times. A shift | |
1227 | sets the value of $1 to the value of $2, the value of | |
1228 | $2 to the value of $3, and so on, decreasing the | |
1229 | value of $# by one. If there are zero positional | |
1230 | parameters, shifting doesn't do anything. | |
1231 | .TP | |
1232 | trap [ action ] signal... | |
1233 | Cause the shell to parse and execute action when any | |
1234 | of the specified signals are received. The signals | |
1235 | are specified by signal number. Action may be null | |
1236 | or omitted; the former causes the specified signal to | |
1237 | be ignored and the latter causes the default action | |
1238 | to be taken. When the shell forks off a subshell, it | |
1239 | resets trapped (but not ignored) signals to the | |
1240 | default action. The trap command has no effect on | |
1241 | signals that were ignored on entry to the shell. | |
1242 | .TP | |
1243 | umask [ mask ] | |
7ab274c2 | 1244 | Set the value of umask (see umask(2)) to the specified octal value. If the argument is omitted, the |
dd00107c MT |
1245 | umask value is printed. |
1246 | .TP | |
1247 | unalias [-a] [name] | |
1248 | If ``name'' is specified, the shell removes that alias. | |
1249 | If ``-a'' is specified, all aliases are removed. | |
1250 | .TP | |
1251 | unset name... | |
1252 | The specified variables and functions are unset and | |
1253 | unexported. If a given name corresponds to both a | |
1254 | variable and a function, both the variable and the | |
1255 | function are unset. | |
1256 | .TP | |
1257 | wait [ job ] | |
1258 | Wait for the specified job to complete and return the | |
1259 | exit status of the last process in the job. If the | |
1260 | argument is omitted, wait for all jobs to complete | |
1261 | and the return an exit status of zero. | |
7ab274c2 | 1262 | .LP |
dd00107c MT |
1263 | .sp 2 |
1264 | .B Command Line Editing | |
1265 | .sp | |
1266 | .LP | |
1267 | When sh is being used interactively from a terminal, the current command | |
1268 | and the command history (see fc in Builtins) can be edited using vi-mode | |
1269 | command-line editing. This mode uses commands, described below, similar | |
1270 | to a subset of those described in the vi man page. | |
1271 | The command set -o vi enables vi-mode editing and place sh into vi | |
1272 | insert mode. | |
1273 | With vi-mode enabled, sh can be switched between insert mode and command | |
1274 | mode. The editor is not described in full here, but will be in a later | |
1275 | document. It's similar to vi: typing <ESC> will throw you into | |
dcd7db01 | 1276 | command VI command mode. Hitting <return> while in command mode |
dd00107c | 1277 | will pass the line to the shell. |